poj3061 Subsequence ,尺取法
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.
尺取法
设置两个指针s,t。
维护一个区间的信息。然后依据指针的移动,更新区间信息。
时间复杂度 O(n).
同类型练习题:
poj 3320 Jessica's Reading Problem
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 100;
int a[maxn]; int main() {
int T, n, S;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &S);
for(int i=1; i<=n; ++i) scanf("%d", &a[i]);
int ans = n+1;
int tmp = 0, s = 1, t = 1;
while(true)
{
while(t<=n && tmp < S) {
tmp += a[t];
t++;
}
if(tmp < S) break;
ans = min(ans, t-s);
tmp -= a[s++];
} if(ans<=n) printf("%d\n", ans);
else printf("0\n");
}
return 0;
}
poj3061 Subsequence ,尺取法的更多相关文章
- POJ3061——Subsequence(尺取法)
Subsequence POJ - 3061 给定长度为n的数列整数a0,a1,a2-an-1以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0. 反复推进区间的开头和末尾,来 ...
- POJ 3061 Subsequence 尺取法
转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ3061 Subsequence 尺取or二分
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ 3061 Subsequence(尺取法)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18145 Accepted: 7751 Desc ...
- POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9050 Accepted: 3604 Descr ...
- poj3061 Subsequence(尺取法)
https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
- 尺取法 poj3061 poj3320
尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...
随机推荐
- STL之效率比較
1.vector 变长一维数组,连续存放的内存块,有保留内存.堆中分配内存: 支持[]操作,高效率的随机訪问: 在最后添加元素时,一般不须要分配内存空间,速度快:在中间或開始操作元素时要进行内存拷贝效 ...
- 锋利Jquery 第一天
之前一直学习,现在终于有时间来整理一下文档了. 以下文章都是自己学习Jquery 的笔记, 希望能留下痕迹,也希望能帮助到您. 好了开始我的Jquery第一天. 我也是从Hello wrod!开始的 ...
- 抓取git的log文件批处理命令示例
@echoset sincedate="2016-04-28 00:00:01" ::变量set beforedate="2016-04-29 00:0 ...
- jmeter的认识
jmeter JMeter是Apache组织开发的开源项目,设计之初是用于做性能测试的,同时它在实现对各种接口的调用方面做的比较成熟,因此,常被用做接口功能测试和性能测试. 它能够很好的支持各种常见接 ...
- 任何抛开业务谈大数据量的sql优化都是瞎扯
周三去某在线旅游公司面试.被问到了一个关于数据量大的优化问题.问题是:一个主外键关联表,主表有一百万数据,外键关联表有一千万的数据,要求做一个连接. 本人接触过单表数据量最大的就是将近两亿行历史数据( ...
- Unity 设置2台摄像机的叠加
如果Camera1和Camera2拍摄的物体各自在摄像机视野内,如果在同一个屏幕上把2件物体都显示出来? 1,Camera1的Depth设置为-1 2,Camera2的Depth设置为0,同时Clea ...
- Debian下签名无法验证
又收集到的新方法 gpg --keyserver subkeys.pgp.net --recv-keys AED4B06F473041FA && apt-key add /root ...
- Eclipse中使用GIT将已提交到本地的文件上传至远程仓库
GIT将已提交到本地的文件上传至远程仓库: 1. 右击项目——Team——Push to Upstream,即可将已保存在本地的文件上传推至GIT远程仓库.
- Python笔记26----正则表达式匹配
1.语法: 2.题目1:数据类型: 如果要选择 time 为2014-12-18那天的数据:采用正则化来处理 代码: import re regex = re.compile(r'^2014-12 ...
- php && 运算符使用说明
“&&” 运算符的用法: ;;echo $test;//输出:000