xtu summer individual 5 D - Subsequence

Subsequence

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on  HDU. Original ID: 3530
64-bit integer IO format: %I64d      Java class name: Main
 
 
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

 
解题:单调队列!马丹,真蛋疼,第一次搞这个。。。。。
 
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cstdlib>

 5 #include <vector>

 6 #include <climits>

 7 #include <algorithm>

 8 #include <cmath>

 9 #define LL long long

10 #define INF 0x3f3f3f3f

11 using namespace std;

12 const int maxn = 100100;

13 int qa[maxn],qb[maxn],h1,h2,t1,t2;

14 int n,m,k,d[maxn],lst1,lst2;

15 int main(){

16     int i,ans;

17     while(~scanf("%d %d %d",&n,&m,&k)){

18         for(i = 1; i <= n; i++)

19             scanf("%d",d+i);

20         lst2 = lst1 = h1 = h2 = 0;

21         t1 = t2 = -1;

22         ans = 0;

23         for(i = 1; i <= n; i++){

24             while(t1 >= h1 && d[qa[t1]] <= d[i]) t1--;

25             qa[++t1] = i;

26             while(t2 >= h2 && d[qb[t2]] >= d[i]) t2--;

27             qb[++t2] = i;

28             while(d[qa[h1]] - d[qb[h2]] > k){

29                 if(qa[h1] < qb[h2]){

30                     lst1 = qa[h1++];

31                 }else lst2 = qb[h2++];

32             }

33             if(d[qa[h1]] - d[qb[h2]] >= m)

34                 ans = max(ans,i-max(lst1,lst2));

35         }

36         printf("%d\n",ans);

37     }

38     return 0;

39 }

40 /*

41 5 0 0

42 1 1 1 1 1

43 5 0 3

44 1 2 3 4 5

45 */
View Code

 

 

你可能感兴趣的:(sequence)