zoukankan      html  css  js  c++  java
  • hdu 3530 (单调队列)

    Subsequence

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

    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
    用两个单调队列维护最大值和最小值
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<set>
    #include<cmath>
    #include<map>
    #include<algorithm>
    using namespace std;
    int p[101010];
    int mmax[101010],mmin[101010];
    int main(){
        int n,m,k;
        while(cin>>n>>m>>k){
            for(int i=1;i<=n;i++) scanf("%d",p+i);
            int head1,tail1,head2,tail2,be=1,mm=0;
            head1=tail1=0;
            head2=tail2=0;
            for(int i=1;i<=n;i++){
                while(head1<tail1&&p[mmax[tail1-1]]<p[i]) tail1--;//维护单调递增
                while(head2<tail2&&p[mmin[tail2-1]]>p[i]) tail2--;//维护单调递减
                mmax[tail1++]=i;
                mmin[tail2++]=i;
                while(head1<tail1&&head2<tail2&&p[mmax[head1]]-p[mmin[head2]]>k){
                    if(mmax[head1]<mmin[head2]) be=mmax[head1++]+1;
                    else be=mmin[head2++]+1;
                }
                if(head1<tail1&&head2<tail2&&p[mmax[head1]]-p[mmin[head2]]>=m){
                    mm=max(mm,i-be+1);
                }
            }
            cout<<mm<<endl;
        }
    }
     
  • 相关阅读:
    MySQL全文索引应用简明教程
    web前端的春天 or 噩梦
    [DeviceOne开发]-手势动画示例分享
    [DeviceOne开发]-土地销售项目源码分享
    [DeviceOne开发]-do_LinearLayout组件使用技巧
    2016年我们重新思考移动互联网创业的风险, 微信还是APP?
    APP技术演化的路
    ReactNative&weex&DeviceOne对比
    what's deviceone
    APP开放源码第一弹《纳豆》
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3997058.html
Copyright © 2011-2022 走看看