csu 1553: Good subsequence

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;
}
时间: 2025-01-04 16:32:46

csu 1553: Good subsequence的相关文章

CSU 1553 Good subsequence(RMQ问题 + 二分)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553 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 m

STL or Force --- CSU 1553: Good subsequence

Good subsequence Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1553 Mean: 给你一个长度为n的序列和一个值k,让你找出一个子序列,满足在这个子序列中max-min的值<=k,求这个子序列最长的长度. analyse: 这题做法很多,直接暴力枚举每一个数为起点. Time complexity: O(n) Source code:  方法一(暴力): // Memory Time //

[ACM] CSU 1553 Good subsequence(尺取法)

题目地址:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1553 给定n的数的序列,求最长连续区间满足区间内的数最大值与最小值的差<=k (尺取法) const int maxn=10010; int num[maxn]; int n,k; int MIN,MAX; int main() { while(scanf("%d%d",&n,&k)!=EOF) { for(int i=1;i<=n;i++) sc

csu 1553: Good subsequence (最长连续子序列)

http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2071&pid=6 题意:有一个由n个数组成的序列 要求出一个满足 max-min<=k 的最长子序列 思路:(听说数据大的情况可以用单调栈解决 但是我只是纯粹暴力了) 首先枚举左界 把max和min都赋值为 a[i] 再枚举右界 每次判断max-min是否小于等于k 求出最大值 #include<cstdio> #include<iostream> #include

csu 1553(RMQ+尺取法)

1553: Good subsequence Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 794  Solved: 287[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 c

2018年省赛热身赛第4场

A:CSU 1547: Rectangle (思维题加一点01背包) B:1548: Design road (思维题 做法:三分找极值) C:1549: Navigition Problem (几何计算+模拟 细节较多) D:1550: Simple String (做得少的思维题,两个字符串能否组成另外一个字符串问题) G:1553: Good subsequence (很奇妙的set模拟题,也可以直接暴力) H:1554: SG Value (巧妙的模拟题,也属于思维题) I:1555:

CSU 1060

1060: Nearest Sequence Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 370  Solved: 118[Submit][Status][Web Board] Description Do you remember the "Nearest Numbers"? Now here comes its brother:"Nearest Sequence".Given three sequences of

csuOJ啊 1553

题目地址:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1553 #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; fo

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)