标签:
Description
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.
Input
The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.
Output
For each the case the program has to print the result on separate line of the output file.if no answer, print 0.
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
比较简单的题,二分也可以,但是尺取法更加高效
/*************************************************************************
> File Name: POJ3061.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年03月30日 星期一 14时40分27秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
const int N = 100100;
int sum[N];
int arr[N];
int main()
{
int t;
int n, s;
scanf("%d", &t);
while (t--)
{
sum[0] = 0;
int ans = inf;
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; ++i)
{
scanf("%d", &arr[i]);
sum[i] = sum[i - 1] + arr[i];
}
int i = 1, j = 1;
while (i <= n && j <= n)
{
while (j <= n && sum[j] - sum[i - 1] < s)
{
++j;
}
if (j <= n)
{
ans = min(ans, j - i + 1);
++i;
}
else
{
break;
}
}
if (ans == inf)
{
ans = 0;
}
printf("%d\n", ans);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/guard_mine/article/details/44750957