E - Subsequence
2015-10-28 18:31
375 查看
LA 3029
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
Many test cases will be given. 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.
Sample Input
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
思路:这道题主要难度在于时间,数据范围是十的五次方,如果采用传统的O(n^2)算法恐怕会力不从心,因此要想到改变枚举方式,只枚举子序列的终点?
使用前缀合sum[i],那么最开始是设j=0,那么开始枚举sum[i]-sum[j],如果找到一个符合条件的子序列,就尝试逐渐增加j,最后也无需把j置零,因为sum数列是递增的.如果前面有sum[i]-sum[j]符合条件,那么任何大于i的k都符合sum[k]-sum[j]>=s
View Code
PS:使用lower_bound()进行查找,如果所查找的数比数列中所有的数都要小,就会返回数组头位置的前一位,如果查找的数比数列中所有的数都要大,那么就会返回数组尾的后一位,原因想一想二分法的原理就知道了
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
Many test cases will be given. 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.
Sample Input
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
思路:这道题主要难度在于时间,数据范围是十的五次方,如果采用传统的O(n^2)算法恐怕会力不从心,因此要想到改变枚举方式,只枚举子序列的终点?
使用前缀合sum[i],那么最开始是设j=0,那么开始枚举sum[i]-sum[j],如果找到一个符合条件的子序列,就尝试逐渐增加j,最后也无需把j置零,因为sum数列是递增的.如果前面有sum[i]-sum[j]符合条件,那么任何大于i的k都符合sum[k]-sum[j]>=s
#include <cstdio> #include <algorithm> #include <iostream> using namespace std; const int maxn=1e5+10; int sum[maxn]; int main() { int n,k; while(scanf("%d%d",&n,&k)!=EOF) { sum[0]=0; int a; for(int i=1;i<=n;i++) {scanf("%d",&a);sum[i]=sum[i-1]+a;} int j=0; int minx=1e9; for(int i=0;i<=n;i++) { j=lower_bound(sum,sum+i,sum[i]-k)-sum; if(j>0) minx=min(minx,i-j+1); } printf("%d\n",minx==1e9?0:minx); } return 0; }
View Code
PS:使用lower_bound()进行查找,如果所查找的数比数列中所有的数都要小,就会返回数组头位置的前一位,如果查找的数比数列中所有的数都要大,那么就会返回数组尾的后一位,原因想一想二分法的原理就知道了
相关文章推荐
- uestc 1222 Sudoku
- IOS UITableView的分隔线多出问题
- easyUi datebox 默认当天
- UIAlertController replaces the UIActionSheet and UIAlertView
- cordys static value 和 read from message
- UIAlertView is deprecated in iOS 8.
- Cannot reload AVD List : Value '280dpi' is not facet-valid with ......---Android studio调试错误
- Frequently Asked Questions - P-thresholds
- Druid使用起步1
- 【UI视觉】100行代码实现微信底部渐变切换效果!!!
- VerQueryValue获取文件-属性-详细信息
- iOS —— SVN出错 Description : The working copy is locked due to a previous error.
- AndroidStudio 问题Gradle sync failed: failed to find Build Tools revision 23.0.0 rc2
- this class is not key value coding-compliant for the key XXX错误的解决方法
- Android Volley完全解析(三),定制自己的Request
- UIAlertController
- NSData 与 NSString,Byte数组,UIImage 的相互转换
- UITableView出现上移/下移64的问题 解析
- 给JSON中put的value=null时,这对key=value会被隐藏掉。
- getRequestDispatcher()与sendRedirect()的区别