Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2518 Accepted Submission(s): 826
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.
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
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3530
题意:给你一个长度为n的数列,要求一个子区间,使得区间的最大值与最小值的差s满足,m<=s<=k,求满足条件的最长子区间
分析:做了前面几题后,这题容易想到用两个单调队列维护当前最值,作为判断条件,如果差值大于k了,就去掉较前面的那个队列元素,并把区间头更新为它的标号+1,这里注意差值小于m并不需要去掉元素,还有更新答案时要先判断是否满足条件才能更新,因为这两条我又wa了n次啊= =
代码:
#include<cstdio>
#include<iostream>
using namespace std;
const int mm=111111;
int a[mm],qa[mm],qb[mm];
int main()
{
int i,n,m,k,ans,l,la,ra,lb,rb;
while(~scanf("%d%d%d",&n,&m,&k))
{
ans=l=la=lb=0,ra=rb=-1;
for(i=1;i<=n;++i)
{
scanf("%d",&a[i]);
while(la<=ra&&a[qa[ra]]>a[i])--ra;
while(lb<=rb&&a[qb[rb]]<a[i])--rb;
qa[++ra]=i;
qb[++rb]=i;
while(a[qb[lb]]-a[qa[la]]>k)
l=(qa[la]<qb[lb])?qa[la++]:qb[lb++];
if(a[qb[lb]]-a[qa[la]]>=m&&a[qb[lb]]-a[qa[la]]<=k)ans=max(ans,i-l);
}
printf("%d\n",ans);
}
return 0;
}