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