zoukankan      html  css  js  c++  java
  • Subsequence

    Subsequence

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

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 22503   Accepted: 9602

    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

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<string>
     4 #include<map>
     5 #include<vector>
     6 #include<cmath>
     7 #include<string.h>
     8 #include<stdlib.h>
     9 #include<stack>
    10 #include<queue>
    11 #include<cstdio>
    12 #define ll long long
    13 const long long MOD=1000000007;
    14 #define maxn 100005
    15 using namespace std;
    16 
    17 int a[maxn];
    18 int n,s;
    19 
    20 
    21 
    22 int main(){
    23     std::ios::sync_with_stdio(false);
    24     int T;
    25     cin>>T;
    26     while(T--){
    27         int n;
    28         cin>>n>>s;
    29         for(int i=1;i<=n;i++){
    30             cin>>a[i];
    31         }
    32         int sum=a[1];
    33         int len=1;
    34         int ans=-1;
    35         int L=1,R=1;
    36         while(L<=R){
    37             if(R==n){
    38                 sum-=a[L++];
    39                 len--;
    40                 if(sum>s){
    41                     ans=ans==-1?len:min(ans,len);
    42                 }
    43             }
    44             else{
    45                 if(sum<s){
    46                     sum+=a[++R];
    47                     len++;
    48                     if(sum>s){
    49                         ans=ans==-1?len:min(ans,len);
    50                     }
    51                 }
    52                 else{
    53                     sum-=a[L++];
    54                     len--;
    55                     if(sum>s){
    56                         ans=ans==-1?len:min(ans,len);
    57                     }
    58                 }
    59             }
    60         }
    61         if(ans==-1) cout<<0<<endl;
    62         else cout<<ans<<endl;
    63 
    64     }
    65 
    66 
    67 }
    View Code
  • 相关阅读:
    走读OpenSSL代码从一张奇怪的证书说起(二)
    从数学到密码学(十八)
    从数学到密码学(十七)
    走读OpenSSL代码从一张奇怪的证书说起(一)
    从数学到密码学(十六)
    从数学到密码学(十五)
    从数学到密码学(十三)
    从数学到密码学(十九)
    从数学到密码学(十四)
    关于Terracotta DSO 安装
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/10066471.html
Copyright © 2011-2022 走看看