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