码迷,mamicode.com
首页 > 其他好文 > 详细

csu 1553: Good subsequence

时间:2015-05-18 09:05:43      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:csu 1553 good subseq

1553: Good subsequence

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 549  Solved: 190
[Submit][Status][Web Board]

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

HINT


求最长连续子序列,该子序列中最大值-最小值《=k

#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;
}


csu 1553: Good subsequence

标签:csu 1553 good subseq

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/45799995

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!