HDOJ 3530 Subsequence
Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3501 Accepted Submission(s): 1131
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
1 1 1 1 1
5 0 3
1 2 3 4 5
Sample Output
5
4
4
Source
Recommend
zhengfeng
两个单调队列,如果差值小于m就继续往里面加值,如果差值大于k,那就删掉排在前面的最值。。。。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100100;
int dmin[maxn],dmax[maxn];
int f[maxn];
int main()
{
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
int i,ans,minhead,maxhead,mintail,maxtail;
int st;
ans=minhead=maxhead=st=0;
mintail=maxtail=-1;
for(i=1;i<=n;i++)
{
scanf("%d",&f);
while(maxhead<=maxtail&&f>f[dmax[maxtail]]) maxtail--;
dmax[++maxtail]=i;
while(minhead<=mintail&&f<f[dmin[mintail]]) mintail--;
dmin[++mintail]=i;
while(f[dmax[maxhead]]-f[dmin[minhead]]>k)
{
if(dmax[maxhead]<=dmin[minhead])
{
st=dmax[maxhead];
maxhead++;
}
else
{
st=dmin[minhead];
minhead++;
}
}
if(f[dmax[maxhead]]-f[dmin[minhead]]>=m)
{
ans=max(ans,i-st);
}
}
printf("%d\n",ans);
}
return 0;
}