Subsequence
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9236 |
|
Accepted: 3701 |
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
Two-minute code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <Set>9#include <map>Ten#include <queue> One#include <stack> A#include <sstream> -#include <iomanip> - using namespacestd; the Const intinf=0x4fffffff; - Const intexp=1e-6; - Const intms=100005; - + intA[ms]; - intSum[ms]; + intn,s; A at intMain () - { - intT; -scanf"%d",&T); - while(t--) - { inscanf"%d%d",&n,&S); -sum[0]=0; toa[0]=0; + for(intI=1; i<=n;i++) - { thescanf"%d",&a[i]); *sum[i]=a[i]+sum[i-1]; $ }Panax Notoginseng if(sum[n]<S) - { theprintf"0\n"); + Continue; A } the intans=N; + for(intI=1; sum[i-1]+s<=sum[n];i++) - { $ intIndex=lower_bound (sum+i,sum+n,sum[i-1]+s)-sum; $Ans=min (ans,index-i+1); - } -printf"%d\n", ans); the } - return 0;Wuyi}
Ruler, take Dafa.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <Set>9#include <map>Ten#include <queue> One#include <stack> A#include <sstream> -#include <iomanip> - using namespacestd; the Const intinf=0x4fffffff; - Const intexp=1e-6; - Const intms=100005; - + intA[ms]; - intn,s; + A intMain () at { - intT; -scanf"%d",&T); - while(t--) - { -scanf"%d%d",&n,&S); in for(intI=0; i<n;i++) -scanf"%d",&a[i]); to intsum=0; + ints=0, t=0; - intans=n+1; the while(1) * { $ while(t<n&&sum<S)Panax Notoginsengsum+=a[t++]; - if(sum<S) the Break; +Ans=min (ans,t-s); Asum-=a[s++]; the } + if(ans>N) -printf"0\n"); $ Else $printf"%d\n", ans); - } - return 0; the}
subsequence poj 30,612 min (nlog N) or ruler (n)