HDU 3530 Subsequence

来源:互联网 发布:淘宝网雷丝高领打底衫 编辑:程序博客网 时间:2024/06/06 00:40

Subsequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7550 Accepted Submission(s): 2555
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
2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU
Recommend
zhengfeng | We have carefully selected several similar problems for you: 3535 3529 3528 3527 3415

//维护两个单调队列#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define N 100010using namespace std;inline int max(int a ,int b) { return a>b?a:b; }int s1[N],s2[N],a[N];int main() {    int n,m,k,top1,top2,last1,last2,tail1,tail2,Ans;    while(scanf("%d%d%d",&n,&m,&k)!=EOF) {        for(int i=1; i<=n; i++) scanf("%d",&a[i]);        memset(s1,0,sizeof(s1));        memset(s2,0,sizeof(s2));        top1 = 0,top2 = 0,tail1 = 0,tail2 = 0;        Ans = 0,last1 = 0,last2 = 0;        for(int i=1; i<=n; i++){            //max            while(top1 < tail1 && a[s1[tail1 - 1]] <= a[i]) tail1--;  //top1最大元素            s1[tail1++] = i;            //min            while(top2 < tail2 && a[s2[tail2 - 1]] >= a[i]) tail2--;  //top2最小元素            s2[tail2++] = i;            while(a[s1[top1]] - a[s2[top2]] > k){                if(s1[top1] < s2[top2])                    last1 = s1[top1++];                else last2 = s2[top2++];            }            if(a[s1[top1]] - a[s2[top2]] >= m)                Ans = max(Ans,i - max(last1,last2));        }        printf("%d\n",Ans);    }    return 0;}
原创粉丝点击