POJ3061-Subsequence
来源:互联网 发布:欧弟老婆淘宝店叫什么 编辑:程序博客网 时间:2024/06/05 07:56
首先是一个nlogn的解法:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000+5;int num[maxn];int sum[maxn];int main(int argc, char const *argv[]) { int t; scanf("%d", &t); while (t--) { int n, s; scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) { scanf("%d", &num[i]); sum[i+1] = sum[i] + num[i]; } if (sum[n] < s) { printf("0\n"); continue; } int res = n; for (int i = 0; sum[i] + s <= sum[n]; i++) { int t = lower_bound(sum + i, sum + n, sum[i] + s) - sum; res = min(res, t - i); } printf("%d\n", res); } return 0;}
另外一种更高效的算法是尺取法,效率为O(n),与上面类似的是斗士在区间上反复对比考量。仔细阅读其实并不难理解。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000+5;int a[maxn];int main(int argc, char const *argv[]) { int t; scanf("%d", &t); while (t--) { int n, s; scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int res = n + 1; int j = 0, k = 0, sum = 0; while (true) { while (k < n && sum < s) { sum += a[k++]; } if (sum < s) break; res = min(res, k - j); sum -= a[j++]; } if (res > n) res = 0; 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 尺取法
- 图片轮番
- 华为在线训练(5)
- ios 设置字体家族
- 陈老师撕B志玲姐姐的热门微博数据分析
- hdu5773The All-purpose Zero
- POJ3061-Subsequence
- 一看就懂的ReactJs入门教程(精华版)
- NVIDIA GPU的Compute Capability一览
- CSS-表格基础记录
- Codeforces 50B Choosing Symbol Pairs
- Android开源:网络框架volley使用
- 【数组9】数组中只出现一次的数字
- PCB板的一些介绍
- 由父子元素margin说开——外边距塌陷(margin-collapse)