poj3061
2016-04-25 21:22
225 查看
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 Source Southeastern Europe 2006 |
[Discuss]
![](http://poj.org/images/home.gif)
Home Page
![](http://poj.org/images/goback.gif)
Go
Back
![](http://poj.org/images/top.gif)
To top
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int n,S; int a[1000010]; void solve() { int t,s,sum,ans; t=s=sum=0; ans=n+1; for(;;) { while(sum<S&&t<n) { sum+=a[t++]; } if(sum<S) break; ans=min(ans,t-s); sum-=a[s++]; } if(ans>n) ans=0; printf("%d\n",ans); } int main() { int t,i; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&S); for(i=0;i<n;i++) scanf("%d",&a[i]); solve(); } return 0; }
相关文章推荐
- 机器学习基石第八讲:noise and error
- JS取整的一些方法
- 堆(heap)和栈(stack)的区别
- 学习进度条
- 摩根士丹利发布2025年区块链技术预测报告
- UOJ 一般图的最大匹配(带花树算法模板)
- LA 4254 Processor (单调队列 + 二分)
- 解决ListView的item监听事件个子控件监听事件冲突问题
- 根据select下拉框选择的值,向form表单添加所要显示的内容的方法
- Impala
- HTTP协议基本知识
- 第八周总结
- 一周总结
- 冲刺阶段第八天
- 数据库 --> MySQL存储引擎介绍
- [HDOJ4325]Flowers(树状数组 离散化)
- antlr.collections.AST.getLine()I异常
- 稀疏矩阵的十字链表存储
- C经典 打印二维数组五个学生三门课的每门课平均成绩及总课程的平均的成绩
- 线程与进程