POJ 3061 Subsequence
2017-10-15 13:59
357 查看
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
Sample Output
Source
Southeastern Europe 2006
t题目要求找一个区间和大于S切长度最小的区间
从头到尾扫一遍
如果当前和小于s 尾指针加1
如果当前和大于等于s 删除第一个元素
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
代码
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
Southeastern Europe 2006
t题目要求找一个区间和大于S切长度最小的区间
从头到尾扫一遍
如果当前和小于s 尾指针加1
如果当前和大于等于s 删除第一个元素
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <cctype> 2 #include <cstdio> 3 4 const int INF=0x3f3f3f3f; 5 const int MAXN=100010; 6 7 int T,n,s,ans; 8 9 int a[MAXN]; 10 11 inline void read(int&x) { 12 int f=1;register char c=getchar(); 13 for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar()); 14 for(;isdigit(c);x=x*10+c-48,c=getchar()); 15 x=x*f; 16 } 17 18 int hh() { 19 read(T); 20 while(T--) { 21 read(n);read(s); 22 int sum=0; 23 ans=INF; 24 for(int i=1;i<=n;++i) read(a[i]),sum+=a[i]; 25 if(s>sum) {printf("0\n");continue;} 26 int head=1,tail=1; 27 sum=a[1]; 28 while(head<=tail) { 29 if(sum<s&&tail<n) sum+=a[++tail]; 30 else sum-=a[head++]; 31 if(sum>=s) 32 if(ans>tail-head+1) ans=tail-head+1; 33 } 34 printf("%d\n",ans); 35 } 36 return 0; 37 } 38 39 int sb=hh(); 40 int main(int argc,char**argv) {;}
代码
相关文章推荐
- POJ 3061 Subsequence (尺取法)
- Poj - 3061 - Subsequence 【最基础的尺取法裸题】
- POJ 3061 Subsequence(尺取法)
- POJ 3061 Subsequence [尺取法] 《挑战程序设计竞赛》3.2
- poj 3061 Subsequence(尺取法)
- poj-3061-Subsequence(二分思想)
- POJ 3061:Subsequence 查找连续的几个数,使得这几个数的和大于给定的S
- poj 3061--Subsequence(尺取法)
- poj 3061 Subsequence
- [POJ 3061]Subsequence
- Greedy:Subsequence(POJ 3061)
- POJ 3061 Subsequence
- POJ 3061 Subsequence(尺取法)
- Poj 3061 Subsequence 队列区间求和
- 【POJ 3061】Subsequence(二分法)
- 挑战3.2.1 Subsequence (poj 3061)
- POJ 3061 Subsequence 尺取法附基础模板
- POJ 3061 - Subsequence
- POJ 3061 Subsequence
- POJ - 3061 Subsequence