Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp
1 second
256 megabytes
standard input
standard output
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
- Each piece should contain at least l numbers.
- The difference between the maximal and the minimal number on the piece should be at most s.
Please help Alexandra to find the minimal number of pieces meeting the condition above.
The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).
The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
7 2 2
1 3 1 2 4 1 2
3
7 2 2
1 100 1 100 1 100 1
-1
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].
For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=2e9;
const ll INF=1e18+,mod=;
int a[N];
struct linetree
{
int maxx[N<<],minn[N<<];
void pushup(int pos)
{
maxx[pos]=max(maxx[pos<<],maxx[pos<<|]);
minn[pos]=min(minn[pos<<],minn[pos<<|]);
}
void build(int l,int r,int pos)
{
if(l==r)
{
maxx[pos]=a[l];
minn[pos]=a[l];
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
void update(int p,int c,int l,int r,int pos)
{
if(l==r)
{
maxx[pos]=c;
minn[pos]=c;
return;
}
int mid=(l+r)>>;
if(p<=mid)
update(p,c,l,mid,pos<<);
else
update(p,c,mid+,r,pos<<|);
pushup(pos);
}
int query(int L,int R,int l,int r,int pos,int flag)
{
if(L<=l&&r<=R)
{
if(flag)
return maxx[pos];
else
return minn[pos];
}
int mid=(l+r)>>;
int ans=-inf;
if(!flag)
ans=inf;
if(L<=mid)
if(flag)
ans=max(ans,query(L,R,l,mid,pos<<,flag));
else
ans=min(ans,query(L,R,l,mid,pos<<,flag));
if(R>mid)
if(flag)
ans=max(ans,query(L,R,mid+,r,pos<<|,flag));
else
ans=min(ans,query(L,R,mid+,r,pos<<|,flag));
return ans;
}
};
linetree tree,dp;
int main()
{
int n,l,s;
scanf("%d%d%d",&n,&s,&l);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
tree.build(,n+,);
dp.build(,n+,);
dp.update(,,,n+,);
for(int i=;i<=n;i++)
{
int st=,en=i-l,ans=-;
while(st<=en)
{
int mid=(st+en)>>;
if(tree.query(mid+,i,,n+,,)-tree.query(mid+,i,,n+,,)<=s)
{
ans=mid;
en=mid-;
}
else
st=mid+;
}
//cout<<ans<<endl;
if(ans==-||ans+>i-l+)
dp.update(i+,inf,,n+,);
else
{
int minn=dp.query(ans+,i-l+,,n+,,);
//cout<<ans+1<<" "<<i-l+1<<" "<<i<<" "<<minn<<endl;
dp.update(i+,minn+,,n+,);
} }
if(dp.query(n+,n+,,n+,,)>=inf)
printf("-1");
else
printf("%d\n",dp.query(n+,n+,,n+,,));
return ;
}
/// dp[i]=min(dp[mid-1]-dp[i-l])+1
Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp的更多相关文章
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
- Codeforces Round #278 (Div. 1) B. Strip multiset维护DP
B. Strip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/487/problem/B De ...
- Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列
B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #765 Div.1 F. Souvenirs 线段树
题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
随机推荐
- Swift - 获取当前系统时间
// 获取当前系统时间 let date = NSDate() let timeFormatter = NSDateFormatter() timeFormatter.dateFormat = &qu ...
- 微信小程序 --- 完成小程序支付功能
最近开发小程序,一直在看小程序的支付.经过一天的努力,小程序支付功能最终实现了. 下面感谢 csdn 博主:千堆雪惹尘埃 发布的 " 小程序与php 实现微信支付 " 原文地址: ...
- 几何+线段交点+spfa(POJ1066)
Treasure Hunt Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Total ...
- 170602、防止sql注入(一)
一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...
- ubuntu video,gdm swith
如果已经安装LightDM和GDM登录显示器.那么在Ubuntu下怎么在各种DM间任意切换呢? 以切换到GDM为例,打开终端,使用命令: sudo dpkg-reconfigure gdm 如果已经安 ...
- CVP沙龙
关于职场: 35岁之后,还去招聘网站投简历? 35岁可能是个分水岭 95后比一些80后还强, 有些80后玻璃心 35岁有的可能已经是VP了 应该深入积累而不是蜻蜓点水 只有第一年成长了,之后是重复劳动 ...
- 洛谷P1156 垃圾陷阱 dp
正解:dp 解题报告: 这儿是传送门! 话说最近怎么神仙们都开始狂刷dp,,,感觉今天写了好多dp的题解的样子?(也就三四道其实× 然后这题,首先看到要么吃要么堆起来就会想到01背包趴?然后就考虑设方 ...
- 洛谷P2024 食物链 [NOI2001] 并查集
正解:并查集 解题报告: 传送门(咕了! 其实没有很难(虽然我是交了三发才过的QAQ 但是一来好久没打并查集了恢复一下智力 二来看着智推里唯一一个蓝就很不爽(,,,虽然做了这题之后又补上了个蓝题QAQ ...
- mysql 约束条件 primary key 主键
primary key字段的值不为空且唯一 约束:not null unique 存储引擎:innodb 对于innodb来说,一张表内必须有一个主键 单列做主键多列做主键(复合主键) 通常都是id字 ...
- 布局-EasyUI Panel 面板、EasyUI Tabs 标签页/选项卡、EasyUI Accordion 折叠面板、EasyUI Layout 布局
EasyUI Panel 面板 通过 $.fn.panel.defaults 重写默认的 defaults. 面板(panel)当做其他内容的容器使用.它是创建其他组件(比如:Layout 布局.Ta ...