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.
  • 相关阅读:
    学习:Radio Button和Check Box
    学习:访问Edit Control的七种方法
    实现:EDIT控件字符个数与长度的计算
    学习:GDI基础
    学习:MFC的CWinApp和CFrameWnd
    学习:远程代码注入
    实现:获取指定进程PID
    学习:远程线程实现DLL注入和shellcode注入以及OD调试原理
    学习:内存映射文件
    实现 Trie (前缀树)
  • 原文地址:https://www.cnblogs.com/Currier/p/6653363.html
Copyright © 2011-2022 走看看