HDU 3530 Subsequence

来源:互联网 发布:2017网络效应判断题 编辑:程序博客网 时间:2024/06/05 12:46
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


 单调队列。
用两个单调队列维护最大值和最小值。

#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<stack>using namespace std;#define ll long long#define getMax(a,b) a>b?a:b#define getMin(a,b) a<b?a:bconst int N=1e+5+5;int q[N],p[N],h1,h2,r1,r2;int a[N];int main(){    int n,m,k,i;    while (~scanf("%d%d%d",&n,&m,&k))        {            h1=h2=r1=r2=0;            int ans=0,cur=1;            for (i=1;i<=n;i++)            {                scanf("%d",&a[i]);            while (h1<r1&&a[q[r1-1]]<a[i])  //递减                r1--;            q[r1++]=i;            while (h2<r2&&a[p[r2-1]]>a[i])  //递增                r2--;            p[r2++]=i;            while (h1<r1&&h2<r2&&a[q[h1]]-a[p[h2]]>k)  //选择下标小的            {                if (q[h1]<p[h2])                    cur=q[h1++]+1;                else                    cur=p[h2++]+1;            }            if (h1<r1&&h2<r2&&a[q[h1]]-a[p[h2]]>=m)                if (ans<i-cur+1)                ans=i-cur+1;            }            printf("%d\n",ans);        }    return 0;}



0 0
原创粉丝点击