zoukankan      html  css  js  c++  java
  • 湖南多校对抗赛---Good subsequence

    题目网址: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;
    }
    






  • 相关阅读:
    Python LED
    vmvare虚拟机经验
    Debian系统简要说明
    Android onclick监听事件打开新界面
    在国外搭个人服务器(顺便访问外网)
    生成指定时间内的 随机日起
    生成指定时间内的日期
    Mac 上查找javahome
    java 学习的网站
    Java 线程
  • 原文地址:https://www.cnblogs.com/zswbky/p/5431984.html
Copyright © 2011-2022 走看看