zoukankan      html  css  js  c++  java
  • HDOJ 3530 Subsequence



    Subsequence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3501    Accepted Submission(s): 1131


    Problem Description
    There is a sequence of integers. Your task is to find the longest subsequence that satisfies the following condition: the difference between the maximum element and the minimum element of the subsequence is no smaller than m and no larger than k.
     

    Input
    There are multiple test cases.
    For each test case, the first line has three integers, n, m and k. n is the length of the sequence and is in the range [1, 100000]. m and k are in the range [0, 1000000]. The second line has n integers, which are all in the range [0, 1000000].
    Proceed to the end of file.
     

    Output
    For each test case, print the length of the subsequence on a single line.
     

    Sample Input
    5 0 0
    1 1 1 1 1
    5 0 3
    1 2 3 4 5
     

    Sample Output
    5
    4
     

    Source
     

    Recommend
    zhengfeng
     


    两个单调队列,如果差值小于m就继续往里面加值,如果差值大于k,那就删掉排在前面的最值。。。。。。。


    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    const int maxn=100100;

    int dmin[maxn],dmax[maxn];
    int f[maxn];

    int main()
    {
        int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        int i,ans,minhead,maxhead,mintail,maxtail;
        int st;

        ans=minhead=maxhead=st=0;
        mintail=maxtail=-1;

        for(i=1;i<=n;i++)
        {
            scanf("%d",&f);

            while(maxhead<=maxtail&&f>f[dmax[maxtail]])  maxtail--;
            dmax[++maxtail]=i;

            while(minhead<=mintail&&f<f[dmin[mintail]])  mintail--;
            dmin[++mintail]=i;

            while(f[dmax[maxhead]]-f[dmin[minhead]]>k)
            {
                if(dmax[maxhead]<=dmin[minhead])
                {
                    st=dmax[maxhead];
                    maxhead++;
                }
                else
                {
                    st=dmin[minhead];
                    minhead++;
                }
            }


            if(f[dmax[maxhead]]-f[dmin[minhead]]>=m)
            {
                ans=max(ans,i-st);
            }
        }

        printf("%d ",ans);
    }

        return 0;
    }

  • 相关阅读:
    HTTP Header 详解
    面试题----网页/应用访问慢突然变慢,如何定位问题
    PHP实现斐波那契数列
    常见的HTTP返回状态值
    通过实例理解单列索引、多列索引以及最左前缀原则
    Btree索引和Hash索引
    遍历和删除文件夹
    面试题之----禁掉cookie的session使用方案
    面试题之----写个函数来解决多线程同时读写一个文件的问题
    heredoc
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350972.html
Copyright © 2011-2022 走看看