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 ...
随机推荐
- android通过数组,流播放声音的方法,音频实时传输
AudioRecord和AudioTrack类是Android获取和播放音频流的重要类,放置在android.media包中.与该包中 的MediaRecorder和MediaPlayer类不同,Au ...
- 拼接的html的onclick事件中无法传递对象给js方法的处理办法
如下: 拼接的html: " onclick=\"valDocName2('"+JSON.stringify(doc).replace(new RegExp(" ...
- log file sync 事件(转)
log file sync log file sync等待时间发生在redo log从log buffer写入到log file期间. 下面对log file sync做个详细的解释. 何时发 ...
- 微信小程序 --- 文件的上传和下载
文件上传 / 文件下载 : wx.uploadFile
- loading图标modal弹窗 和jquery ajax的关系
在ajax配置中 ,async:false,非异步,modal窗口会失效,只有重新设置为async:true,或者删除async的设置,则loading的模态框才能展示出来 loading图标的模态框 ...
- Android--aapt命令
1.aapt l[ist] [-v] [-a] file.{zip,jar,apk} 释义:列出压缩文件中的内容 aapt l xxx.apk:简单的罗列压缩文件中每一项的内容 aapt l -v x ...
- 转!!CMPP 网关错误码说明
http://www.163duanxin.com/msg/1753.htm CMPP错误码说明 与中国移动代码的对应关系. MI::zzzzSMSC返回状态报告的状态值为EXPIREDMJ:zz ...
- 【Python项目篇】【爬妹子图】
#-*- coding:utf-8 -*- import urllib import urllib2 from bs4 import beautifulsoup4 #获取标签下的内容 #打开网页,获取 ...
- 【云安全与同态加密_调研分析(7)】安全技术在云计算中的安全应用分析——By Me
我司安全技术在云计算中的安全应用分析 1. 基于云计算参考模型,分析我司安 ...
- 美图秀秀DBA谈MySQL运维及优化
美图秀秀DBA谈MySQL运维及优化 https://mp.weixin.qq.com/s?__biz=MzI4NTA1MDEwNg==&mid=401797597&idx=2& ...