POJ 3061 subsequence ruler method, a cock dick O (n) algorithm

Source: Internet
Author: User

Subsequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9050 Accepted: 3604

Description

A sequence of n positive integers (< n <), each of the them less than or equal 10000, and a positive integer S (S <) 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. The program have to read the numbers N and S, separated by a interval, from the first line. The numbers of the sequence is given in the second line of the "test case", separated by intervals. The input would finish with the end of file.

Output

The the program have to print the result on separate line of the output File.if no answer, print 0.

Sample Input

210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5

Sample Output

23
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 100001Const intinf=0x7fffffff;//infinitely LargeintA[MAXN];intMain () {intT; CIN>>T;  while(t--)    {        intn,s; CIN>>n>>s;  for(intI=0; i<n;i++) {cin>>A[i]; } ll sum=a[0]; intst=0, en=0; intminn=inf;  while(en!=n&&st<=en) {            //cout<<st<< "" <<en<< "" <<sum<<endl;            if(sum>=s) {Minn=min (minn,en-st+1); Sum-=A[st]; St++; }            if(sum<s) {en++; Sum+=A[en]; }        }        if(minn==inf) cout<<"0"<<Endl; Elsecout<<minn<<Endl; }    return 0;}

POJ 3061 subsequence ruler method, a cock dick O (n) algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.