Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp
2017-03-02 16:50
435 查看
D. Strip
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
Examples
input
output
input
output
Note
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.
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
Examples
input
7 2 2 1 3 1 2 4 1 2
output
3
input
7 2 2 1 100 1 100 1 100 1
output
-1
Note
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+10,M=1e6+10,inf=2e9; const ll INF=1e18+10,mod=2147493647; int a ; struct linetree { int maxx[N<<2],minn[N<<2]; void pushup(int pos) { maxx[pos]=max(maxx[pos<<1],maxx[pos<<1|1]); minn[pos]=min(minn[pos<<1],minn[pos<<1|1]); } void build(int l,int r,int pos) { if(l==r) { maxx[pos]=a[l]; minn[pos]=a[l]; return; } int mid=(l+r)>>1; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); 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)>>1; if(p<=mid) update(p,c,l,mid,pos<<1); else update(p,c,mid+1,r,pos<<1|1); 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)>>1; int ans=-inf; if(!flag) ans=inf; if(L<=mid) if(flag) ans=max(ans,query(L,R,l,mid,pos<<1,flag)); else ans=min(ans,query(L,R,l,mid,pos<<1,flag)); if(R>mid) if(flag) ans=max(ans,query(L,R,mid+1,r,pos<<1|1,flag)); else ans=min(ans,query(L,R,mid+1,r,pos<<1|1,flag)); return ans; } }; linetree tree,dp; int main() { int n,l,s; scanf("%d%d%d",&n,&s,&l); for(int i=1;i<=n;i++) scanf("%d",&a[i]); tree.build(1,n+1,1); dp.build(1,n+1,1); dp.update(1,0,1,n+1,1); for(int i=1;i<=n;i++) { int st=0,en=i-l,ans=-1; while(st<=en) { int mid=(st+en)>>1; if(tree.query(mid+1,i,1,n+1,1,1)-tree.query(mid+1,i,1,n+1,1,0)<=s) { ans=mid; en=mid-1; } else st=mid+1; } //cout<<ans<<endl; if(ans==-1||ans+1>i-l+1) dp.update(i+1,inf,1,n+1,1); else { int minn=dp.query(ans+1,i-l+1,1,n+1,1,0); //cout<<ans+1<<" "<<i-l+1<<" "<<i<<" "<<minn<<endl; dp.update(i+1,minn+1,1,n+1,1); } } if(dp.query(n+1,n+1,1,n+1,1,1)>=inf) printf("-1"); else printf("%d\n",dp.query(n+1,n+1,1,n+1,1,1)); return 0; } /// dp[i]=min(dp[mid-1]-dp[i-l])+1
相关文章推荐
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(线段树+离散化优化DP)
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
- Codeforces Round #271 (Div. 2) E. Pillars(线段树优化DP)
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
- Codeforces Round #426 (Div. 2) D 线段树优化dp
- Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP
- Codeforces Round #271 (Div. 2)(dp,线段树good)(很好的一场cf)
- SPOJ1748 - SEQPAR2 二分答案 DP优化 单调队列+线段树
- poj 1769 Minimizing maximizer 线段树优化的dp
- poj 1769 Minimizing maximizer 线段树优化的dp
- Codeforces Round #271 (Div. 2)E. Pillars(dp+线段树优化)
- ZOJ 3349 Special Subsequence(线段树优化DP)
- ZOJ-3349 Special Subsequence 线段树优化DP
- HDU 3698 Let the light guide us 线段树求区间优化dp
- 最长上升子序列nlogn通解(dp之线段树优化)
- codeforces 271E 离散化+线段树,dp优化
- zoj 2900 DP(线段树优化)
- Codeforces Round #121 (Div. 1) / 191A Dynasty Puzzles (dp&优化)
- 线段树和单调队列优化DP---POJ2373解题报告
- HDU 4521 2013腾讯编程马拉松初赛第四场 小明系列问题——小明序列(dp思想+线段树优化)