Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9384    Accepted Submission(s): 3148


 

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

 

题意:求最大的区间长度使得区间的最大与最小差在[m,k]之间。

分析:求区间的最大值和最小值我们可以用两个单调队列去维护,对于如何保证它的最大值和最小值差在m和k之间,其实这里有点思维,如果我们发现max-min>k,我们就要更新它到<=k,但对于<m,我们不管,因为<m在后面会重新更新,加值便会改变,但>k就成为了定局,在更新最大值和最小值时候需要记录位置pos,pos的记录具体看代码把,一看就懂。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int N=100005;
deque<int>b,s;
int sum[N],v[N];
int main()
{
int n,m,k;
while(~scanf("%d%d%d",&n,&m,&k))
{
b.clear();
s.clear();
int ans=0,pos=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&v[i]);
while(!b.empty()&&v[i]>=v[b.back()])
b.pop_back();
while(!s.empty()&&v[i]<=v[s.back()])
s.pop_back();
b.push_back(i);
s.push_back(i);
while(v[b.front()]-v[s.front()]>k)
{
if(b.front()<s.front())
{
pos=b.front();
b.pop_front();
}
else
{
pos=s.front();
s.pop_front();
}
}
if(v[b.front()]-v[s.front()]>=m)
{
ans=max(ans,i-pos);
}

}
printf("%d\n",ans);
}
return 0;
}