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

 

转载于:https://www.cnblogs.com/switch-waht/p/11396798.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值