求较小区间差并且区间和大于某个值的问题

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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int sum[100005];
int main()
{
    int n,N,S;
    cin>>n;
    while(n--)
    {
        cin>>N>>S;
        for(int t=0;t<N;t++)
        {
            int k;
            scanf("%d",&k);
            sum[t]=sum[t-1]+k;
        }
        if(sum[N-1]<S)
        {
            printf("0\n");
        }
        else 
        {
            int flag;
            int a=N-1;
            for(int t=0;sum[t-1]+S<=sum[N-1];t++)
            {
           int index=lower_bound(sum+t,sum+N-1,sum[t-1]+S)-sum;                  
            a=min(a,index-t+1);
       }
            
            printf("%d\n",a);
        }
    }
    return 0;
}
View Code

 

posted @ 2018-11-08 21:48  black_hole6  阅读(222)  评论(0)    收藏  举报