【尺取法】poj 3061 Subsequence
2016-04-13 12:49
323 查看
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
题意:给定长度为n的数列(都为正整数) , 求出总和不超过S的连续子序列的长度的最小值,如果不存在则输出0 ;
写法:先递推出所有项的和, 确定出子序列的起点s,就可以二分查找到末端;
PS:这个题输入量巨大,就算用二分也会导致擦边而过,跑了860MS,当然还可以优化的,但是用了输入挂,跑了16MS。对于巨量的数据这个还是很好用 的。
AC代码:
尺取法:
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
题意:给定长度为n的数列(都为正整数) , 求出总和不超过S的连续子序列的长度的最小值,如果不存在则输出0 ;
写法:先递推出所有项的和, 确定出子序列的起点s,就可以二分查找到末端;
PS:这个题输入量巨大,就算用二分也会导致擦边而过,跑了860MS,当然还可以优化的,但是用了输入挂,跑了16MS。对于巨量的数据这个还是很好用 的。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std ; int a[100000000+5] , sum[100000000+5]; inline bool scan_d(int &num) { char in;bool IsN=false; in=getchar(); if(in==EOF) return false; while(in!='-'&&(in<'0'||in>'9')) in=getchar(); if(in=='-'){ IsN=true;num=0;} else num=in-'0'; while(in=getchar(),in>='0'&&in<='9'){ num*=10,num+=in-'0'; } if(IsN) num=-num; return true; } int main() { int t ; cin>>t; int n , S ; while(t--) { scan_d(n); scan_d(S); for(int i = 0 ; i<n ; i++) { scan_d(a[i]); } for(int i = 0 ; i < n ; i++) { sum[i+1] = sum[i] + a[i] ; } if(sum < S) { printf("0\n"); continue ; } int res = n ; //*s为起点 for(int s = 0 ; sum[s] + S <=sum ; s++) { int t = lower_bound(sum+s , sum + n , sum[s]+S) - sum ;//*折半求出后结点位置; res = min(res , t - s );//*比较前后结点距离, } cout << res <<endl ; } return 0 ; }
尺取法:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> using namespace std; int a[100000000] , S, n ; int main() { int m ; cin>>m; while(m--) { cin>>n>>S; for(int i = 0 ; i < n ; i++) { cin>>a[i]; } int res = n+1 ; int s , ans , t ; s = ans = t = 0 ; //*ans代表和, s代表前节点,t代表后节点,res=t-s就是区间长度; for(;;) { while(t<n&&ans<S) { ans+=a[t++]; } if(ans < S) break; res = min(res , t -s); ans-=a[s++]; } if(res > n) printf("0\n"); else printf("%d\n",res); } return 0 ; }
相关文章推荐
- Android handler Thread 修改UI Demo
- 静态检测内存泄露Analyze--Value stored to 'dataArr' during its initialization is never read
- request中的各种方法
- zoj3929 Deque and Balls 【找规律】
- 修改UITabBarButton位置等
- pod安装AFNetworking3.0遇到dependency were found, but they required a higher minimum deployment target
- WPF 实现指定UI控件截图
- String.valueOf() 方法的使用
- C#新开一个线程取到数据,如何更新到主线程UI上面
- 第三方开源库:nineoldandroid:ValueAnimator 动态设置textview的高
- 解决UINavigationController 自定义LeftBarButtonItem左边空隙问题
- iOS UITextField控件
- Java并发包中的同步队列SynchronousQueue实现原理
- LeetCode 95, 96. Unique Binary Search Trees i. ii
- [LeetCode] Word Frequency 单词频率
- 【View】之【SimpleWaveView】可多色可刷新的加速球、进度球【demo】
- WebSphere+db2使用PROPAGATION_REQUIRES_NEW事务策略因为的系
- 【View】之【SimplePillarsView】可多色可圆角柱状图【demo】
- c 语言unique 实现
- IOS生成GUID