zoukankan      html  css  js  c++  java
  • poj3061 Subsequence(尺取)

    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

    Source

    Southeastern Europe 2006
     
     
    题目大意:
    尺取。练练手。
    program rrr(input,output);
    var
      a:array[0..100010]of longint;
      t,n,s,i,j,k,sum,ans:longint;
    function min(a,b:longint):longint;
    begin
       if a<b then exit(a) else exit(b);
    end;
    begin
       assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
       readln(t);
       for k:=1 to t do
          begin
             readln(n,s);
             for i:=1 to n do read(a[i]);
             j:=0;sum:=0;ans:=n+1;
             for i:=1 to n do
                begin
                   while (j<n) and (sum<s) do begin inc(j);sum:=sum+a[j]; end;
                   if sum<s then break;
                   ans:=min(ans,j-i+1);
                   sum:=sum-a[i];
                end;
             if ans>n then writeln(0) else writeln(ans);
          end;
       close(input);close(output);
    end.
  • 相关阅读:
    雅虎35条优化黄金守则
    安装入门
    NPOI 2.0 Excel读取显示
    STL算法
    MVVM框架avalon在兼容旧式IE
    Asp.Net MVC3.0网站统计登录认证的在线人数
    Windows Server 服务器安全配置
    SignalR的服务器广播
    angularjs + seajs构建Web Form3
    MVC应用程序显示上传的图片
  • 原文地址:https://www.cnblogs.com/Currier/p/6653363.html
Copyright © 2011-2022 走看看