poj3061
用一个队列就可以解决,别忘了没答案要输出0。
View Code
#include <stdio.h>
#define maxn 100005
int f[maxn], n, s;
void input() {
int i;
scanf("%d%d", &n, &s);
for (i = 0; i < n; i++)
scanf("%d", &f[i]);
}
int main() {
//freopen("D:\\t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--) {
input();
int i, sum = 0, l = 0, ans = maxn;
for (i = 0; i < n; i++) {
sum += f[i];
while (sum - f[l] >= s) {
sum -= f[l];
l++;
}
if (i - l + 1 < ans && sum >= s)
ans = i - l + 1;
}
if (ans == maxn)
printf("0\n");
else
printf("%d\n", ans);
}
return 0;
}