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的连续子序列的 ...
随机推荐
- Chisel Tutorial(一)——Chisel介绍
Chisel是由伯克利大学公布的一种开源硬件构建语言,建立在Scala语言之上,是Scala特定领域语言的一个应用,具有高度參数化的生成器(highly parameterized generator ...
- Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)
题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...
- java连接mysql数据库增删改查操作记录
1. 连接数据库.得到数据库连接变量 注意连接数据库的时候 (1)打开DB Browser 新建一个Database Driver,注意加入Driver JARs的时候加入的包,我的是mysql-co ...
- angularjs1-路由
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 0x37 容斥原理与莫比乌斯函数
多重集的组合数公式得记下.cf451E就是这个的裸题 #include<cstdio> #include<iostream> #include<cstring> # ...
- poj--2631--Roads in the North(树的直径 裸模板)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2389 Accepted: 117 ...
- Winform WPF 窗体显示位置
WinForm 窗体显示位置 窗体显示的位置首先由窗体的StartPosition决定,FormStartPosition这个枚举值由如下几种情况 // 摘要: // 窗体的位置由 System.Wi ...
- ZBrush中物体的显示与隐藏
在ZBrush®中除了遮罩功能可以对局部网格进行编辑外,通过显示和隐藏局部网格也可以对局部进行控制.选择网格的控制都是手动操作,在软件中并没有相应的命令进行操作.选择局部网格的工作原理也很简单,即被选 ...
- ZBrush软件特性之Draw
ZBrush®中的Draw绘制调色板包括了当前绘图的修改和控制工具,能改变工具大小.形状.强度.不透明度和其他一些功能. Draw Size(绘制大小):设置画笔的外形尺寸,调节ZBrush绘制笔刷圆 ...
- Java中使用MD5加密的简单实现
import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorith ...