poj 3061
来源:互联网 发布:2017年伤感网络歌曲 编辑:程序博客网 时间:2024/05/27 18:17
题目链接:点击打开链接
给定长度为n的数列整数a0,a1,。。。an-1以及整数S,求出总和不小于S的连续子序列长度的最小值。如果解不存在,则输出零。
刷挑战。。。
尺取和二分,果然还是尺取块。。。
#include<iostream>#include<algorithm>#include<string>#include<queue>#include<cmath>#include<vector>#include<stdlib.h>#include<iomanip>#include<list>#include<stack>#include<memory.h>#include<ctype.h>using namespace std;typedef long long ll;const int maxn = 1e5 + 5;int num[maxn];int n, m;void solve() {//尺取int ans = n + 1;int start = 0, t = 0, sum = 0;for (;;) {while (t < n&&sum < m) {sum += num[t++];}if (sum < m)break;ans = min(ans, t - start);sum -= num[start++];}if (ans > n)ans = 0;printf("%d\n", ans);}int main() {int t;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++) {scanf("%d", &num[i]);}solve();}}/*int main() {//二分int t;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);int x;for (int i = 0; i <n; i++) {scanf("%d", &x);num[i + 1] = num[i] + x;}int ans = n;if (num[n] < m)ans = 0;else {for (int i = 0; num[i] + m <= num[n];i++) {x = lower_bound(num + i, num + n, num[i] + m)-num;//ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)//算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。ans = min(ans, x-i);}}printf("%d\n", ans);}}*/
阅读全文
0 0
- poj 3061
- poj 3061
- poj 3061
- poj-3061
- POJ 3061
- poj 3061
- poj 3061
- POJ----3061
- poj 3061
- 【POJ】3061
- poj 3061
- POJ 3061
- 【poj】 3061
- 尺取法专题 POJ 3061 POJ 3320 POJ 2566
- poj 3061 Subsequence
- poj 3061(高效枚举)
- POJ-3061-Subsequence
- POJ - 3061 Subsequence
- 突然想写点什么~
- 史上最详细的Android Studio系列教程
- HashMap源码分析
- 二分查找
- 循环矩阵傅里叶对角化
- poj 3061
- C++的类静态成员
- mysql 定时备份数据库脚本
- 不带头结点的链表操作及其逆置
- unity2017破解失败
- 1071. 小赌怡情(15)
- Java 反射(Reflection)
- 经典进程同步问题(一)——生产者、消费者问题
- 注册驱动并建立数据库的连接