• 1553: Good subsequence (很奇妙的set模拟题,也可以直接暴力)


    1553: Good subsequence

          Time Limit: 2 Sec     Memory Limit: 256 Mb     Submitted: 895     Solved: 335    


    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

    Source

    给你一个序列,长度为n
    然后给一个数字k
    问你符合要求的子段的最大长度是多少?
    符合要求的子段:子段的最大值和最小值的差小于等于k
    分析:
    利用set模拟该过程,或者直接暴力
    不知道怎么描述这个过程
    不过按照代码模拟一下样例1应该就明白了
     
    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define max_v 10005
    using namespace std;
    int a[max_v];
    set<int> s;
    int main()
    {
        int n,k;
        while(~scanf("%d %d",&n,&k))
        {
            s.clear();
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
            }
            int j=1;
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                s.insert(a[i]);
                if(*s.rbegin()-*s.begin()>k)
                {
                    s.erase(s.find(a[j]));
                    j++;
                }
                ans=max(ans,i-j+1);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    /*
    给你一个序列,长度为n
    然后给一个数字k
    问你符合要求的子段的最大长度是多少?
    符合要求的子段:子段的最大值和最小值的差小于等于k
    
    分析:
    利用set模拟该过程,或者直接暴力
    不知道怎么描述这个过程
    不过按照代码模拟一下样例1应该就明白了
    */
  • 相关阅读:
    02-三种布局方式/触屏事件/BFC
    02-单点登录(移动端)
    02-转>>chunk-vendors过大导致首屏加载太慢的优化
    15-转>pc端和h5端多页面配置
    14-转>publicPath
    04-GitHub上上传自己的项目
    03-合并到master后打tag
    final关键字
    重载(Overloading)与覆写(Override)的区别?
    腾讯微博
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9498673.html
Copyright © 2020-2023  润新知