标签:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12311 | Accepted: 5169 |
Description
Input
Output
Sample Input
2 10 15 5 1 3 5 10 7 4 9 2 8 5 11 1 2 3 4 5
Sample Output
2 3
Source
#include <cstdio>
#include <algorithm>
using namespace std;
const int INF = 0x7fffffff;
const int MAXN = 100000+5;
int a[MAXN];
int n, s;
void solve()
{
//尺取法
int l = 0, r = 0; //初始化左右指针
int sum = 0, res = INF;
while(1){
while(r < n && sum < s){ //不断移动右指针,直到满足条件
sum += a[r++];
}
if(sum < s) //条件无法满足,终止
break;
res = min(res, r-l); //更新结果,相应区间为[l, r)
sum -= a[l++]; //移动左指针
}
printf("%d\n", res == INF ? 0 : res);
}
int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &s);
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
solve();
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/inmoonlight/p/5750053.html