zoukankan      html  css  js  c++  java
  • 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.

    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.

    我先说几句 我*************

    不到四十行的sbdp写了两天。。。。。。。到现在还没想懂

    不说了泪奔

    #include<cstdio>
    #include<cstring>
    #include<set>
    using namespace std;
    #define N 200010
    #define inf 1000000009
    int n,S,l,last=1;
    int dp[N],mn[3*N],a[N];
    multiset<int> s;
    set<int> v;
    void update(int l,int r,int x,int pos,int num)
    {
        if(l==r)
        {
            mn[x]=num;
            return;
        }
        int mid=(l+r)>>1;
        if(pos<=mid) update(l,mid,x<<1,pos,num);
        else update(mid+1,r,x<<1|1,pos,num);
        mn[x]=min(mn[x<<1],mn[x<<1|1]);
    }
    int query(int l,int r,int x,int a,int b)
    {
        if(l>b||r<a) return inf;
        if(l>=a&&r<=b) return mn[x];
        int mid=(l+r)>>1;
        return min(query(l,mid,x<<1,a,b),query(mid+1,r,x<<1|1,a,b));
    }
    int main()
    {
        memset(mn,0x3f3f,sizeof(mn));
        scanf("%d%d%d",&n,&S,&l);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        /*
            dp[i]:到i位置能分多少段 
        */
        for(int i=1;i<=n;i++)
        {
            s.insert(a[i]);
            while(*s.rbegin()-*s.begin()>S) 
            {
                s.erase(s.find(a[last]));
                last++;
            }
            if(i-last+1>=l&&last==1) dp[i]=1; else 
            dp[i]=query(1,n,1,last-1,i-l)+1;
            update(1,n,1,i,dp[i]);
        }
        printf("%d
    ",dp[n]>=inf?-1:dp[n]);
        return 0;
    }
    View Code
  • 相关阅读:
    Devexpress根据条件单元格变色以及根据条件设置单元格可编辑-记录
    批量更新事物加回滚
    批量更新
    Devexpress GridControl无限高度惹得祸
    C# 多语言国际化问题中的 CultureInfo
    配置linux服务器的防火墙,以CENTOS 为例(转载)
    关于Java的程序运行提醒
    用Eclipse跑Hadoop程序的注意事项
    Hadoop平台-错误收集附解决方案!
    unity3d之物体克隆
  • 原文地址:https://www.cnblogs.com/19992147orz/p/6337572.html
Copyright © 2011-2022 走看看