POJ 3061 Subsequence

来源:互联网 发布:淘宝拍视频用什么软件 编辑:程序博客网 时间:2024/06/05 09:35

Subsequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16812 Accepted: 7141

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

210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5

Sample Output

23

Source

Southeastern Europe 2006

题目大意:给你n个数字,再给你一个S,求大于S的连续子序列的最小长度

尺取法

#include<iostream>#include<algorithm>using namespace std;int num[100010];int main(){//freopen("in.txt","r",stdin);int t;cin>>t;while(t--){int n,s;cin>>n>>s;for(int i=0;i<n;i++) cin>>num[i];int i=0,j=0;int sum=0;int ans=n+1;while(true){while(j<n&&sum<s) sum+=num[j++];if(sum<s) break;ans=min(j-i,ans);sum-=num[i++];}if(ans>n) ans=0;cout<<ans<<endl;}}