Give you a sequence of n numbers, and a number k you should find the max length of Good subsequence. Good subsequence is a continuous subsequence of the given sequence and its maximum value - minimum value<=k. For example n=5, k=2, the sequence ={5, 4, 2,
3, 1}. The answer is 3, the good subsequence are {4, 2, 3} or {2, 3, 1}.
There are several test cases.
Each test case contains two line. the first line are two numbers indicates n and k (1<=n<=10,000, 1<=k<=1,000,000,000). The second line give the sequence of n numbers a[i] (1<=i<=n, 1<=a[i]<=1,000,000,000).
The input will finish with the end of file.
For each the case, output one integer indicates the answer.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#define Mod 1000000007
#define ll long long
#define N 10200
#define INF 1010010010
using namespace std;
int a[N];
int n,k;
int main() {
// freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&k)) {
for(int i=0; i<n; i++) {
scanf("%d",&a[i]);
}
int Max=1;
int l=0;
int ma=a[0],mi=a[0];
int xl=0,xr=0;
for(int i=0; i<n; i++) {
int ma=a[i],mi=a[i];
int j;
for(j=i+1; j<n; j++) {
if(ma<a[j])
ma=a[j];
if(mi>a[j])
mi=a[j];
if(ma-mi>k)
break;
}
Max=max(Max,j-i);
}
printf("%d\n",Max);
}
return 0;
}