POJ 3061 Subsequence【尺取法】
来源:互联网 发布:安全加密软件 编辑:程序博客网 时间:2024/04/30 03:45
原题链接
思路:尺取法,lower_bound
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> using namespace std;int t, n, s, sum[1000002], a[1000002], ans;int main(){ scanf("%d", &t); while(t--){ scanf("%d %d", &n, &s); for(int i = 1; i <= n; i++){ scanf("%d", &a[i]); } sum[0] = 0; for(int i = 1; i <= n; i++){ sum[i] = sum[i - 1] + a[i]; } if(sum[n] < s){ printf("0\n"); continue; } ans = n; for(int i = 1; sum[i] + s <= sum[n]; i++){ int j = lower_bound(sum + i + 1, sum + n + 1, sum[i] + s) - sum; ans = min(ans, j - i); } printf("%d\n", ans); } return 0;}
还可以再进行优化,代码为:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> using namespace std;int t, n, s, sum, a[1000002], ans;int main(){ scanf("%d", &t); while(t--){ scanf("%d %d", &n, &s); for(int i = 0; i < n; i++){ scanf("%d", &a[i]); } int t1 = 0, t2 = 0, sum = 0, ans = n + 1; while(1){ while(sum < s && t2 < n) sum += a[t2++]; if(sum < s) break; ans = min(ans, t2 - t1); sum -= a[t1++]; } if(ans == n + 1) printf("0\n"); else printf("%d\n", ans); } return 0;}
1 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(尺取法)
- Linux网络配置
- 循环语句(for循环,while循环,do-while循环)
- git命令之git mergetool vi非正常退出.swp删除不了的问题
- VS2010-MFC下在状态栏中添加进度条用于显示当前文件的读取进度
- ubuntu中ssh-server的安装与开机启动
- POJ 3061 Subsequence【尺取法】
- unity 下载纹理,并设置成指定的尺寸
- ESP8266的SDK开发入门教程
- uva371 Ackermann Functions
- Activity的生命周期和启动模式
- LTE (5G)是如何更块的
- 关于C的小程序-2
- bzoj 2820: YY的GCD (反演)
- HDU