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 }

 

posted @ 2013-04-15 18:22  Jack Ge  阅读(255)  评论(0编辑  收藏  举报