zoukankan      html  css  js  c++  java
  • Subsequence

     
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15006   Accepted: 6350

    Description

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

    Input

    The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

    Output

    For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

    Sample Input

    2
    10 15
    5 1 3 5 10 7 4 9 2 8
    5 11
    1 2 3 4 5

    Sample Output

    2
    3
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=100100;
    int a[maxn];
    int main(){int T,n,S;scanf("%d",&T);
        while(T--){scanf("%d%d",&n,&S);
            for(int i=1;i<=n;++i)scanf("%d",&a[i]);
            int ans=n+1,tmp=0,s=1,t=1;//ans:the_length,tmp:mark_the_sum,t:back_now,s:front_now
            while(true){// make an ongoing runing
                while(t<=n&&tmp<S)tmp+=a[t],t++;//if has not visted all num already|and|the sum has not bigger than the given S
                //just add it ,and renew the back_now
                if(tmp<S) break;//if tmp<S,just means that t>n and it can not satisfy the given S,just no_solution_case,just break
                ans=min(ans,t-s);tmp-=a[s++];//renew the best solution,and lower it so that it may have better_section than this
                //why this method is right,because the section must be successive and if from 1 to i have not safisfy the given S,from j to i(1<j<i)can not safisfy the given S too
                //and if it satisfy ,maybe the final_several_nums are large,and cut it can be better 
            }if(ans<=n)printf("%d
    ",ans);else printf("0
    ");//if the ans is inside n(just it is in the correctly_section(from 1 to n),just printf the ans
            //or it must be beyond the correctly_section,it means no_solution,just printf 0
        }return 0;}

    program is just above ,the program is the best language

  • 相关阅读:
    FreeNAS的安装使用记录
    slackware中配置命令行下的分辨率
    创建类的理由
    软件开发人员的入门级书单
    Pow(x, n)
    Group Anagrams
    Rotate Image
    Permutations
    Multiply Strings
    Combination Sum II
  • 原文地址:https://www.cnblogs.com/muzu/p/7149183.html
Copyright © 2011-2022 走看看