题目网址:http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2071&pid=6
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
题意解析:找出给出数列中其字串最小值最大值之差不小于给定数值的最长子列的长度
遍历就行了,但是不能最暴力的查找方式稍微优化就好了,见下面代码。。。
不过以前好像做过一道类似的题目,应该能归结为一种算法,以后加上。。。。。。。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
long long int a[10005],n,k;
while( cin>>n>>k )
{
long long int coun=1;
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
long long int sum=0;
for(int i=0;i<n;i++)
{
int t=1;
long long int max=a[i],min=a[i];
for(int j=i+1;j<n;j++)
{
if(a[j]>max)
max=a[j];
if(a[j]<min)
min=a[j];
t=j-i+1;
if(max-min<=k)
{
coun = coun < t ? t : coun;
}
else
break;
}
}
cout<<coun<<endl;
}
return 0;
}