码迷,mamicode.com
首页 > 其他好文 > 详细

xtu summer individual 5 D - Subsequence

时间:2014-08-07 00:22:07      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   for   

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

 
解题:单调队列!马丹,真蛋疼,第一次搞这个。。。。。
 
bubuko.com,布布扣
 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

 

 

xtu summer individual 5 D - Subsequence,布布扣,bubuko.com

xtu summer individual 5 D - Subsequence

标签:style   blog   http   color   java   os   io   for   

原文地址:http://www.cnblogs.com/crackpotisback/p/3895911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!