Description
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}.
Input
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.
Output
For each the case, output one integer indicates the answer.
Sample Input
5 2 5 4 2 3 1 1 1 1
Sample Output
3 1
//HDU1171:Big Event in HDU
#include <cstdio>#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int a[10005];
int main()
{
int n,k,i,j;
while(scanf("%d%d",&n,&k)!=EOF)
{
for (i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
/// sort(a+1,a+1+n); 不能手残排序
int max;
int min;
int count;
int ans=0;
for (i=1;i<=n;i++)
{
count=1;
max=min=a[i];
for (j=i+1;j<=n;j++)
{
if (a[j]>max)
max=a[j];
if (a[j]<min)
min=a[j];
if (max-min>k) break; //很重要的一个剪枝
else
count++;
}
if (ans<count)
ans=count;
}
printf("%d\n",ans);
}
return 0;
}