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

    Subsequence

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


    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
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 const int maxn=100010;
     8 
     9 int minQue[maxn],maxQue[maxn];
    10 int minLeft,minRight;
    11 int maxLeft,maxRight;
    12 int num[maxn];
    13 
    14 int main(){
    15 
    16     //freopen("input.txt","r",stdin);
    17 
    18     int n,m,k;
    19     while(~scanf("%d%d%d",&n,&m,&k)){
    20         minLeft=minRight=0;
    21         maxLeft=maxRight=0;
    22         int ans=0,now=1;
    23         for(int i=1;i<=n;i++){
    24             scanf("%d",&num[i]);
    25             while(minLeft<minRight && num[minQue[minRight-1]]>num[i])
    26                 minRight--;
    27             minQue[minRight++]=i;
    28             while(maxLeft<maxRight && num[maxQue[maxRight-1]]<num[i])
    29                 maxRight--;
    30             maxQue[maxRight++]=i;
    31 
    32             while(minLeft<minRight && maxLeft<maxRight && num[maxQue[maxLeft]]-num[minQue[minLeft]]>k){
    33                 if(maxQue[maxLeft]<minQue[minLeft])
    34                     now=maxQue[maxLeft++]+1;
    35                 else
    36                     now=minQue[minLeft++]+1;
    37             }
    38             if(minLeft<minRight && maxLeft<maxRight && num[maxQue[maxLeft]]-num[minQue[minLeft]]>=m){
    39                 if(ans<i-now+1)
    40                     ans=i-now+1;
    41             }
    42         }
    43         printf("%d\n",ans);
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    冒泡排序python实现
    mysql主从配置
    函数当作参数传递
    使用xml.dom.minidom创建xml
    php得到所有的汉字
    Cannot load D:/phpenv/php/php548/php5apache2_2.dll错误解决
    php新特性 traits 简单方法复用
    【转】在 Windows 下为 PHP 5.4 安装 PEAR、PHPUnit 及 phpDoc2
    wxpython基础框架
    MFC中文档视图框架和文档模板之间的关系
  • 原文地址:https://www.cnblogs.com/jackge/p/3022609.html
Copyright © 2011-2022 走看看