voidsolve(){ memset(sum, 0 ,sizeof(sum)); for(int i = 0; i < n; i++) { sum[i + 1] = sum[i] + a[i]; } if(sum[n] < s) { printf("0\n"); return ; } int res = n; for(int i = 0; sum[i] + s <= sum[n];i++) { int t = lower_bound(sum + i, sum + n, sum[i] + s) - sum; res = min(res, t - i); } printf("%d\n", res); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
voidsolve(){ int res = n + 1; int b = 0, t = 0, sum = 0; for(;;) { while(t < n && sum < s) { sum += a[t++]; } if(sum < s) { break; } res = min(res, t - b); sum -= a[b++]; } if(res > n) { res = 0; } printf("%d\n", res); }