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
很简单的一道题,尺取法就是对数组保存起点和终点,通过根据实际情况交替推进两个端点得到答案。
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;
int a[100010];
int main()
{
int T;
cin >> T;
while (T--)
{
int n, S;
cin >> n >> S;
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
int ans = n + 1;
int s = 0, t = 0, sum = 0;
while (1)
{
while (t < n && sum < S)
sum += a[t++];
if (sum < S)
break;
ans = min(ans, t-s);
sum -= a[s++];
}
if (ans > n)
ans = 0;
cout << ans << endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/god_weiyang/article/details/47683155