Description
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 维护一个队列的和,记下最小的长度。#include<iostream> #include<stdio.h> using namespace std; int a[100100]; int min(int a,int b) { return a<b?a:b; } int main() { int n,s; int sum,minl; int i,j,l; while(scanf("%d%d",&n,&s)!=EOF) { sum=0; l=0; minl=100001; for(i=0;i<n;i++) { scanf("%d",&a[i]); } j=0; for(i=0;i<n;i++) { sum+=a[i]; l++; while(sum>=s) { if(sum>=s) { minl=min(minl,l); } sum-=a[j++]; l--; } } if(minl<100001) { printf("%d\n",minl); } else { printf("0\n"); } } return 0; }