poj3061---Subsequence
来源:互联网 发布:php移动文件函数 编辑:程序博客网 时间:2024/06/05 02:39
Subsequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9358 Accepted: 3767
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
分析:这是一道用的尺取法,首先将所有的数字存在一个数组a里面,sum数组里面存的sum[1]=a[0],sum[2]=a[0]+a[1],,,,,,,,sum[n]=a[0]+,,,a[n];另外二分搜索法中lower_bound()函数的使用参考http://blog.csdn.net/niushuai666/article/details/6734403的博客
int t=lower_bound(sum+j,sum+n,sum[j]+s)-sum; //返回的找到sum[j]+s的上界,再减去j的位置得到的就是他们的和为s的位置
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[100050],sum[100051];int main(){ int n,s; int test; cin>>test; while(test--) { cin>>n>>s; for(int i=0;i<n;i++) cin>>a[i]; memset(sum,0,sizeof(sum)); for(int i=0;i<n;i++) //将它们的和存在一个sum数组里面 sum[i+1]=sum[i]+a[i]; if(sum[n]<s) { printf("0\n"); continue; } int res=n; for(int j=0;sum[j]+s<=sum[n];j++) { int t=lower_bound(sum+j,sum+n,sum[j]+s)-sum; if(res>t-j) { res=t-j; } } printf("%d\n",res); } return 0;}
0 0
- POJ3061 Subsequence
- POJ3061 Subsequence
- POJ3061 Subsequence
- poj3061---Subsequence
- POJ3061-Subsequence
- poj3061 Subsequence
- POJ3061-Subsequence
- POJ3061-Subsequence
- poj3061 Subsequence
- 【poj3061】Subsequence
- poj3061:Subsequence
- poj3061 Subsequence 尺取法
- poj3061 Subsequence ,尺取法
- POJ3061 Subsequence (追逐法)
- [尺取法] poj3061 Subsequence
- POJ3061 Subsequence 尺取法
- POJ3061-Subsequence【尺取法】
- poj3061 Subsequence 尺取法
- VB操作Excel
- springMVC快速入门(转)
- CSS实战手册的一些笔记(1)
- ruby 實變數
- /*c++中二维数组的输入正常,但是输出有问题,感觉就是输入进去但是没有保存下 */
- poj3061---Subsequence
- Javascript 严格模式详解
- 你的java/c/c++程序崩溃了?揭秘段错误(Segmentation fault)(3)
- alter table新增字段操作究竟有何影响?(上篇)
- Python 图片处理-图片翻转
- 性能更好的js动画实现方式——requestAnimationFrame
- [leetcode]Count Primes
- java synchronized详解
- 关于 许式伟谈Go Erlang并发编程差异