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.
  • 相关阅读:
    C风格字符串大写转小写
    指针的引用在函数中的应用
    Debug和Release区别
    差分约束系统(System Of Difference Constraints)
    poj_2299UltraQuickSort && poj_1804Brainman
    计算几何基础——矢量和叉积
    推荐ALGORITHM专题
    后缀数组之倍增法
    静态邻接表的简单实现
    归并排序&&树状数组求逆序数
  • 原文地址:https://www.cnblogs.com/Currier/p/6653363.html
Copyright © 2011-2022 走看看