## 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.

## 思路&代码

```/*Subsequence
Time Limit: 1000 MS Memory Limit: 65536K
Sources: Southeastern Europe 2006*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

int T,n,s,a[100005],l,r,mid,ans;

bool ok(){
int sum=0;
for(int i=1;i<=mid;i++) sum+=a[i];
if(sum>=s) return 1;
for(int i=1;i<=n-mid;i++){
sum-=a[i];sum+=a[i+mid];
if(sum>=s) return 1;
}
return 0;
}

int main(){
cin>>T;
while(T--){
cin>>n>>s;
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++) cin>>a[i];
l=1,r=n,ans=0;
while(l<r){
mid=(l+r)/2;
if(ok()){
r=mid;
ans=mid;
}else{
l=mid+1;
}
}
cout<<ans<<endl;
}
return 0;
}```

This site uses Akismet to reduce spam. Learn how your comment data is processed.