1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cctype> 5 #include <algorithm> 6 using namespace std; 7 8 int Read(){//快读模板,数据较大 9 char c; 10 int ans = 0; 11 c = getchar(); 12 while(!isdigit(c)) 13 c=getchar(); 14 while(isdigit(c)){ 15 ans = ans*10+c-48; 16 c = getchar(); 17 } 18 return 0; 19 } 20 int main(){ 21 while(cin>>n)//多组数据 22 { 23 memset(sum,0,sizeof(sum));//初始化数组 24 ans=def; 25 S=Read(); 26 for(int i=1;i<=n;i++){//读入以及前缀和处理 27 a[i]=Read(); 28 sum[i]=sum[i-1]+a[i]; 29 } 30 int head=0,tail=1; 31 for(tail=1;tail<=n;tail++) 32 { 33 while(sum[tail]-sum[head]>=S) 34 { 35 if(sum[tail]-sum[head]>=S) 36 ans=min(ans,tail-head); 37 head++; 38 } 39 } 40 printf("%d\n",(ans==def)?0:ans); 41 } 42 return 0; 43 }
原文:https://www.cnblogs.com/taotianzhufang/p/14376025.html