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
题目意思,就是给你两个数列,要求找到一个最长子序列,要求,最大与最小的值的差别大于m小于k
//建两个队列,一个最大值,一个最小值 ,如果两个极值之差,比K要大,就要去掉,最小值和最大值中的,最小坐标,来进行更新,如果两个极值之差比m小,就更加入新值不用处理,如果比m大就是合法壮态,可以更新,
#include <iostream>
#include <stdio.h>
#include<cmath>
using namespace std;
int n,m,k,num[1000000];
int queuemax[1000000];
int queuemin[1000000];
int maxx(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
int i,ans,j,frontmin,frontmax,rearmin,rearmax,lastmin,lastmax;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
frontmin=0;rearmin=0;
frontmax=0;rearmax=0;
lastmin=0;lastmax=0;
ans=0;
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
for(i=1;i<=n;i++)
{
while(frontmin<rearmin&&num[queuemin[rearmin-1]]>num[i])//最小优先队列插入
{
rearmin--;
}
//printf("%d %dqueumin\n",rearmin,i);
queuemin[rearmin++]=i;
while(frontmax<rearmax&&num[queuemax[rearmax-1]]<num[i])//最大优先队列的插入
{
rearmax--;
}
queuemax[rearmax++]=i;
// printf("%d %dqueumin\n",rearmin,i);
while(num[queuemax[frontmax]]-num[queuemin[frontmin]]>k)
{
if(queuemax[frontmax]<queuemin[frontmin])//选取下标最小的更新
{
lastmax=queuemax[frontmax];
frontmax++;
}
else
{
lastmin=queuemin[frontmin];
frontmin++;
}
}
if(num[queuemax[frontmax]]-num[queuemin[frontmin]]>=m)//符合条件就更新
{
ans=maxx(ans,i-maxx(lastmin,lastmax));
}
}
printf("%d\n",ans);
}
return 0;
}