CodeForces - 488D (Strip)

Posted switch-waht

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 488D (Strip)相关的知识,希望对你有一定的参考价值。

      Strip

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.

 

题意:把一组长度为N数列分为连续的若干个数列,且满足:1,每个子数列的长度>=L,最大值减最小值<=S,求最小的子数列数目,若无法拆分,输出-1。

 

dp+ST;

技术图片
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
int S,N,L;
const int CON=1e5+5;
int maxn[CON][20],minn[CON][20];
int a[CON];
int dp[CON];//dp[i]表示包括i在内的最小情况. 
int query(int x,int y)//查询[x,y]间的最值 

    int k=log2(y-x+1);
    return max(maxn[x][k],maxn[y-(1<<k)+1][k])-min(minn[x][k],minn[y-(1<<k)+1][k]);

int main()

    while(scanf("%d%d%d",&N,&S,&L)!=EOF)
    
        for(int i=1;i<=N;i++)
        
            scanf("%d",&a[i]);
            maxn[i][0]=a[i];
            minn[i][0]=a[i];
        
        memset(dp,0x3f3f3f3f,sizeof dp);
        int shang=log2(N);
        for(int j=1;j<=shang;j++)
        
            for(int i=1;i+(1<<j)-1<=N;i++)
            
                maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
                minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
            
        
        int pre=0;
        dp[0]=0;
        for(int i=1;i<=N;i++)
        
            while(i-pre>=L&&query(pre+1,i)>S||dp[pre]==0x3f3f3f3f)//(i-pre>=L) => [pre+1,i]=L;
            //长度满足但最值相差大,则pre++;pre前无值,pre++; 
            pre++;
            if(i-pre>=L)
            dp[i]=min(dp[pre]+1,dp[i]);
        
        if(dp[N]==0x3f3f3f3f)
        printf("-1\n");
        else 
        printf("%d\n",dp[N]);
    
    return 0;
 
View Code

 

以上是关于CodeForces - 488D (Strip)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #740 (Div. 2) D1. Up the Strip (simplified version)

Codeforces Round #740 (Div. 2) D1. Up the Strip (simplified version)

筛法应用CF1558B - Up the Strip

筛法应用CF1558B - Up the Strip

筛法应用CF1558B - Up the Strip

Python:地图中的Strip返回该Strip