POJ 3061 Subsequence
来源:互联网 发布:sql having count(*) 编辑:程序博客网 时间:2024/06/03 14:04
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
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
題意:
給定長度為n的數列整數,求出總和不小於S的連續子序列的長度的最小值。如果無解輸出0。
二分查找lower_bound()的使用:
函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~
返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置
pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。
時間複雜度 O(n*log(n)):
#include<iostream>#include<cstring>#include<algorithm>#include<vector>#include<functional>#include<cstdio>#include<cmath>using namespace std;int n, S;int a[100005];int sum[100005];void solve(){ for (int i = 0; i < n; i++)//* { sum[i + 1] = sum[i] + a[i]; }//為了配合lower_bound()越界 if (sum[n] < S) { cout << "0" << endl; return; } int res = n; for (int s = 0; sum[s] + S <= sum[n]; s++) { int t = lower_bound(sum + s, sum + n, sum[s] + S) - sum; //函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的 res = min(res, t - s); } cout << res << endl;}int main(){ int t; cin >> t; while (t--) { cin >> n >> S; for (int i = 0; i < n; i++) { cin >> a[i]; } solve(); } return 0;}
時間複雜度 O(n):
可以一直遞推取得最後答案。
核心伪代码:
while(1)
{
步骤一:s到t表示当前子区间,如果不够S,就sum+=a[t++],满足S后,更新res;
if (sum < S)//不存在剩余的解
break;
步骤二:sum-=a[s++],更新区间S,继续步骤一;
}
#include<iostream>#include<cstring>#include<algorithm>#include<vector>#include<functional>#include<cstdio>#include<cmath>using namespace std;int n, S;int a[100005];int sum[100005];void solve(){ int res = n + 1; int s = 0, t = 0, sum = 0;//s到t表示當前子區間 for (;;) { while (t < n&&sum < S) { sum += a[t++]; } if (sum < S)//解不存在或已全部判断完毕 break; res = min(res, t - s); sum -= a[s++]; } if (res > n) { res = 0; } cout << res << endl;}int main(){ int t; cin >> t; while (t--) { cin >> n >> S; for (int i = 0; i < n; i++) { cin >> a[i]; } solve(); } return 0;}
- poj 3061 Subsequence
- POJ-3061-Subsequence
- POJ - 3061 Subsequence
- POJ:3061 Subsequence
- poj 3061 Subsequence
- poj 3061 Subsequence
- POJ 3061 Subsequence
- poj 3061 Subsequence
- poj 3061 Subsequence
- POJ 3061 Subsequence
- POJ 3061 Subsequence
- poj 3061 Subsequence
- poj 3061 Subsequence
- poj 3061 Subsequence
- POJ-3061 Subsequence
- poj 3061 Subsequence
- POJ 3061 Subsequence
- POJ 3061 Subsequence《《前加后去,,,》》
- VMWare启动centos 7时报内部错误
- ORACLE_参数笔记
- Laravel使用php artisan migrate报错: [PDOException] SQLSTATE[42S02]: Base table or view not found: 114
- Java的log4J完美日志篇
- 开始装逼
- POJ 3061 Subsequence
- sklearn(四)----标准化数据,这里normalization就是scaling
- spring-cloud-ribbon负载均衡(二)
- [FUNC]星级评分函数
- 算数基本原理(唯一分解定理)
- 开通了csdn博客
- 踩坑日记--Integer没设默认值的教训
- D
- MFC的使用——在共享DLL中使用MFC、在静态库中使用