zoukankan      html  css  js  c++  java
  • CodeForces

          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
    ");
            else 
            printf("%d
    ",dp[N]);
        }
        return 0;
    } 
    View Code
  • 相关阅读:
    设计模式的读书笔记
    腾讯历年笔试题
    原始套接字的简单tcp包嗅探
    一些有意思,有深度,容易错的代码收集
    sizeof的用法的一些归纳
    一道有趣的题目
    C++的一些设计注意点
    Gazebo Ros入门
    机器学习之多变量线性回归(Linear Regression with multiple variables)
    机器学习之单变量线性回归(Linear Regression with One Variable)
  • 原文地址:https://www.cnblogs.com/switch-waht/p/11396798.html
Copyright © 2011-2022 走看看