• Subsequence


    题目链接:

    http://poj.org/problem?id=3061

    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
     
     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 const int maxn = 100010;
     5 
     6 int A[maxn], B[maxn];
     7 
     8 int main(void)
     9 {
    10     int n, S;
    11     while (~scanf("%d%d", &n, &S))
    12     {
    13         for (int i = 1; i <= n; i++) 
    14             scanf("%d", &A[i]);
    15         B[0] = 0;
    16         for (int i = 1; i <= n; i++) 
    17             B[i] = B[i - 1] + A[i];
    18         int ans = n + 1;
    19         int i = 1;
    20         for (int j = 1; j <= n; j++)
    21         {
    22             if (B[i - 1] > B[j] - S) 
    23                 continue; // 没有满足条件的i,换下一个j
    24             while (B[i] <= B[j] - S) 
    25                 i++; // 求满足B[i-1]<=B[j]-S的最大i
    26             ans = min(ans, j - i + 1);
    27         }
    28         printf("%d
    ", ans == n + 1 ? 0 : ans);
    29     }
    30     return 0;
    31 }
    View Code
  • 相关阅读:
    Git的commit your changes or stash them before you can merge
    php面试题汇总一(基础篇附答案)
    php面试题汇总二(基础篇附答案)
    php面试题汇总三(基础篇附答案)
    php面试题汇总四(基础篇附答案)
    nodejs 后台服务启动
    解决failed to push some refs to
    读书计划
    spring cloud 学习
    spring IOC
  • 原文地址:https://www.cnblogs.com/biu-biu-biu-/p/5786924.html
Copyright © 2020-2023  润新知