LA 2678 Subsequence
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
Many test cases will be given. 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 there isn't such a subsequence, print 0 on a line by itself.
Sample Input
10 15 5 1 3 5 10 7 4 9 2 8 5 11 1 2 3 4 5
Sample Output
2 3
1 #pragma comment(linker,"/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <queue> 6 #include <cstring> 7 #include <iostream> 8 #include <algorithm> 9 using namespace std; 10 #define INF 0x7fffffff 11 #define mod 1000000007 12 #define ll long long 13 #define maxn 100005 14 #define pi acos(-1.0) 15 //const int maxk = 10 + 1, inf = ~0U >> 2; 16 int n, m, ans,s; 17 int a[maxn]; 18 int main(){ 19 int t; 20 while (~scanf("%d%d",&n,&m)){ 21 s = 0; t = 0; 22 for (int i = 0; i < n; i++){ 23 scanf("%d", &a[i]); 24 if(s<m)s += a[i],t++; 25 } 26 int tt = t; 27 int j = 0; 28 while (s - a[j] >= m&&j<t-1){ 29 s -= a[j]; 30 j++; 31 } 32 t = t - j; 33 if (s < m){ printf("0\n"); continue; } 34 for (int i = tt; i < n; i++){ 35 s += a[i]; 36 int j = i - t; 37 s -= a[j]; j++; 38 while (s - a[j] >= m&&j<i)s-=a[j],j++; 39 t = i - j+1; 40 } 41 printf("%d\n", t); 42 } 43 return 0; 44 }
浙公网安备 33010602011771号