湖南多校对抗赛---Good subsequence
2015-03-29 08:51
381 查看
题目网址:http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2071&pid=6
3, 1}. The answer is 3, the good subsequence are {4, 2, 3} or {2, 3, 1}.
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.
题意解析:找出给出数列中其字串最小值最大值之差不小于给定数值的最长子列的长度
遍历就行了,但是不能最暴力的查找方式稍微优化就好了,见下面代码。。。
不过以前好像做过一道类似的题目,应该能归结为一种算法,以后加上。。。。。。。
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; }
相关文章推荐
- 湖南多校对抗赛(2015.03.28)CSU1547~1556 题解
- Contest2089 - 湖南多校对抗赛(2015.05.31) Swipe(csu1648)
- 湖南多校对抗赛(2015.05.10)(国防科大学校赛决赛-Semilive)CSU1609-1618
- 湖南多校对抗赛(2015.4.6)C
- 湖南多校对抗赛(2014.03.23)JAG Practice Contest 2013
- Contest2058 - 湖南多校对抗赛(2014.05.11)&& NENU ACM 2014-05-18 总结
- 湖南多校对抗赛3.28 D Simple String
- Contest2089 - 湖南多校对抗赛(2015.05.31) Swipe(csu1648)
- Contest2073 - 湖南多校对抗赛(2015.04.06)
- 2017年湖南多校对抗赛第10场-COJ1930-roads
- 湖南多校对抗赛3.28 J - Jerry's trouble
- 湖南多校对抗赛(D-D)
- 湖南多校对抗赛(2015.4.6) A
- Contest2073 - 湖南多校对抗赛(2015.04.06)
- 湖南多校对抗赛(2015.05.03)Problem B: War
- 湖南多校对抗赛(J - J)
- 湖南多校对抗赛(2015.4.6)B
- 湖南多校对抗赛(2015.03.15)9题题解 ABCEFGHJK
- Contest2073 - 湖南多校对抗赛(2015.04.06)
- 湖南多校对抗赛(2015.05.03)Problem A: Twenty-four point