POJ - 3061 Subsequence

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
 1 #include <iostream>
 2 using namespace std;
 3 #include<string.h>
 4 #include<set>
 5 #include<stdio.h>
 6 #include<math.h>
 7 #include<queue>
 8 #include<map>
 9 #include<algorithm>
10 #include<cstdio>
11 #include<cmath>
12 #include<cstring>
13 #include <cstdio>
14 #include <cstdlib>
15 #include<stack>
16 #include<vector>
17 long long  a[110000];
18 int main()
19 {
20     long long  t;
21     cin>>t;
22     while(t--)
23     {
24         memset(a,0,sizeof(a));
25         int n,m;
26         cin>>n>>m;
27         long long  sum=0;
28         for(int i=1;i<=n;i++)
29         {
30             scanf("%lld",&a[i]);
31             sum+=a[i];
32         }
33         if(sum<m)
34         {
35             cout<<"0"<<endl;
36            continue;
37         }
38         sum=0;
39         int temp;
40         for(int i=n;i>=1;i--)
41         {
42             sum+=a[i];
43             if(sum>=m)
44             {
45                 temp=i;
46                 break;
47             }
48         }
49         int min1;
50         min1=n-temp+1;
51         int kaishi=1,jieshu=0;
52         sum=0;
53         while(1)
54         {
55 
56             if(sum<m)
57             {
58                 while(sum<m&&jieshu<=n)
59                 {
60                     sum+=a[++jieshu];
61                 }
62                 min1=min(min1,jieshu-kaishi+1);
63             }
64             else
65             {
66                 min1=min(min1,jieshu-kaishi+1);
67                 sum-=a[kaishi++];
68             }
69             //cout<<kaishi<<"_"<<jieshu<<"_"<<min1<<endl;
70             if(kaishi==temp)
71                 break;
72         }
73         cout<<min1<<endl;
74     }
75     return 0;
76 }
View Code

 

posted @ 2017-12-03 18:12  小小超plus  阅读(135)  评论(0编辑  收藏  举报