zoukankan      html  css  js  c++  java
  • Subsequence

    Subsequence

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


    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
    #pragma GCC optimize(2)
    #include <bits/stdc++.h>
    
    using namespace std;
    const int maxn=1e5+108;
    typedef long long ll;
    
    int n,m,k;
    int c[maxn];
    int max_queue[maxn],min_queue[maxn];
    
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("1.txt","r",stdin);
    #endif
        while(scanf("%d%d%d",&n,&m,&k)!=EOF){
            int max_head=1,max_tail=0,min_head=1,min_tail=0,res=0;
            int f=0;
            for(register int i=1;i<=n;++i){
                scanf("%d",&c[i]);
                while(max_head<=max_tail&&c[i]>=c[max_queue[max_tail]])max_tail--;
                while(min_head<=min_tail&&c[i]<=c[min_queue[min_tail]])min_tail--;
                max_queue[++max_tail]=i;
                min_queue[++min_tail]=i;
                while(c[max_queue[max_head]]-c[min_queue[min_head]]>k){
                    f=min(min_queue[min_head],max_queue[max_head]);
                    if(f==min_queue[min_head]){
                        ++min_head;
                    }
                    if(f==max_queue[max_head]){
                        ++max_head;
                    }
                }
                if(c[max_queue[max_head]]-c[min_queue[min_head]]>=m)res=max(res,i-f);
            }
            printf("%d
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    Python的垃圾回收机制
    标准库
    常用数据库命令备忘录(持续增量更新)
    Springboot配置excludePathPatterns不生效问题 (2020-06-28 22:21)
    Android 子线程无法刷新UI界面
    如何实现Java线程的 阻塞/唤醒(和暂停/继续 类似)
    Android Studio 如何获取 text文本内容
    Css设置最优先
    CentOS7下MySQL服务启动失败原因及解决方法
    Js/Jquery获取input file的文件名
  • 原文地址:https://www.cnblogs.com/czy-power/p/11455046.html
Copyright © 2011-2022 走看看