Subsequence
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10889 | Accepted: 4503 |
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
代码:
/*
题上是让求一个较短的连续子段的和大于等于s的值,把这个子段的长度输出!
所以它可能是任意一个子段,第一次是以1为起点的所有的子段,第2个是以2为起点
的所有的子段,一直循环,直到起点比较大(大到加到n都还是小于ss)!在每一段内
用二分进行搜索,搜到后把该位置到起点的距离的差值的大小与res的值进行比较,
选取较小的(也就是找所有的满足条件的最短的子段长度),输出就OK了!
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n,ss;
int a[100005];
int sum[100005];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(sum,0,sizeof(sum));
scanf("%d%d",&n,&ss);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum[i+1]=sum[i]+a[i];
}
if(sum[n]<ss)
{
printf("0\n");
continue;
}
int res=n;
for(int s=0;sum[s]+ss<=sum[n];s++)
{
int t=lower_bound(sum+s,sum+n,sum[s]+ss)-sum;
res=min(res,t-s);
}
printf("%d\n",res);
}
return 0;
}