Codeforces Round #278 (Div. 1) B. Strip

B. 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.

Sample test(s)
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.

思路: dp[i] 表示 以 i 结尾,最前可以覆盖到哪里

这个可以用二分+线段树搞出来,然后ans1[i] 表示,前 i 个合并的最少分组

对于第i 个点,其合法的覆盖范围是 dp[i] ~ i-l+1 , 那么 ans1[i] = 1+min(ans[j]) ,  

dp[i]-1 <= j <= i-l  ;我们只要用线段树维护 ans1[] 就好了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define maxn 100010
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;

int Min[maxn*3],Max[maxn*3] ;
int dp[maxn],a[maxn] ;
int ql,qr,v1,v2,n,s;

void build(int L,int R,int o)
{
    if(L==R)
    {
        Max[o]=Min[o]=a[L] ;
        return ;
    }
    int mid=(L+R)>>1 ;
    build(L,mid,o<<1) ;
    build(mid+1,R,o<<1|1) ;
    Min[o]=min(Min[o<<1],Min[o<<1|1]) ;
    Max[o]=max(Max[o<<1],Max[o<<1|1]) ;
}
void find(int L,int R,int o )
{
    if(ql<=L&&qr>=R)
    {
        v1=max(Max[o],v1) ;
        v2=min(Min[o],v2) ;
        return ;
    }
    int mid=(L+R)>>1 ;
    if(ql<=mid) find(L,mid,o<<1) ;
    if(qr>mid) find(mid+1,R,o<<1|1) ;
}

void update(int L,int R,int o)
{
    if(L==R)
    {
        Min[o]=v1;
        Max[o]=v2;
        return ;
    }
    int mid=(L+R)>>1;
    if(ql<=mid) update(L,mid,o<<1) ;
    else update(mid+1,R,o<<1|1) ;
    if(Min[o<<1] < Min[o<<1|1])
    {
        Min[o]=Min[o<<1] ;
        Max[o]=Max[o<<1] ;
    }
    else
    {
        Min[o]=Min[o<<1|1] ;
        Max[o]=Max[o<<1|1] ;
    }
}
void find1(int L,int R,int o)
{
    if(ql<=L&&qr>=R)
    {
        if(Min[o]<v1)
        {
            v2=Max[o] ;
            v1=Min[o] ;
        }
        return ;
    }
    int mid=(L+R)>>1 ;
    if(ql<=mid) find1(L,mid,o<<1) ;
    if(qr>mid) find1(mid+1,R,o<<1|1) ;
}
bool check(int &L,int &R)
{
    ql=L;
    qr=R;
    v1=-INF;
    v2=INF;
    find(1,n,1) ;
    if(v1-v2<=s) return true;
    return false;
}
int ans1[maxn];
int main()
{
    int i,j,k,m;
    int L,R,mid,ans,l;
    while(scanf("%d%d%d",&n,&s,&l) != EOF)
    {
        for( i = 1 ; i <= n ;i++)
            scanf("%d",&a[i]) ;
        if(l>n)
        {
            puts("-1") ;
            continue;
        }
        build(1,n,1) ;
        for(i = l ; i <= n ;i++)
        {
            L=1;
            R=i-l+1;
            ans=-1;
            while(L<=R)
            {
                mid=(L+R)>>1 ;
                if(check(mid,i)){
                        R=mid-1;
                        ans=mid;
                }
                else L =mid+1;
            }
            dp[i]=ans;
        }
        memset(Min,INF,sizeof(Min)) ;
        for(i = l ; i <= n ;i++)
        {
            if(dp[i]==1) ans1[i]=1;
            else if(dp[i]==-1)ans1[i]=INF;
            else {
                ql=dp[i]-1;
                qr=i-l;
                v1=INF;
                if(ql<=qr) find1(1,n,1) ;
                if(v1 != INF)
                {
                    ans1[i]=ans1[v2]+1;
                }
                else ans1[i]=INF;
            }
            if(ans1[i] != INF)
            {
                ql=i;
                v1=ans1[i];
                v2=i;
                update(1,n,1) ;
            }
           // cout<<i<<" "<<ans1[i]<<endl;
        }
        ans=ans1[n] ;
        if(ans==INF)ans=-1;
        cout<<ans<<endl;
    }
    return 0 ;
}
View Code
原文地址:https://www.cnblogs.com/20120125llcai/p/4115909.html