poj2823单调队列认知
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 62930 | Accepted: 17963 | |
Case Time Limit: 5000MS |
Description
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Window position | Minimum value | Maximum value |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
Output
Sample Input
- 8 3
- 1 3 -1 -3 5 3 6 7
Sample Output
- -1 -3 -3 -3 3 3
- 3 3 5 5 6 7
- #include<cstdio>
const int N=1e6+88;
int a[N],q[N];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
if(k==1)
{
for(int i=1; i<=n; ++i) printf("%d ",a[i]);
puts("");
for(int i=1; i<=n; ++i) printf("%d ",a[i]);
puts("");
return 0;
}
int h=1,t=1;
q[1]=1;
for(int i=2; i<=n; ++i)
{
if(i-q[h]==k) ++h;
if(a[i]>a[q[t]]) q[++t]=i;
else
{
while(t>=h&&a[i]<=a[q[t]]) q[t--]=i;
++t;
}
if(i>=k) printf("%d ",a[q[h]]);
}
puts("");
h=1,t=1,q[1]=1;
for(int i=2; i<=n; ++i)
{
if(i-q[h]==k) ++h;
if(a[i]<a[q[t]]) q[++t]=i;
else
{
while(t>=h&&a[i]>=a[q[t]]) q[t--]=i;
++t;
}
if(i>=k) printf("%d ",a[q[h]]);
}
puts("");
}
poj2823单调队列认知的更多相关文章
- poj2823 单调队列初步
什么是单调队列:头元素一直是队列当中的最大值,队列中的值按照递减顺序排列,可以从末尾插入一个元素,或从两段删除元素 1.插入元素,为了保证队列的单调性(这里假设为递减性),在插入元素v时要将对位的元素 ...
- 刷题向》POJ2823 单调队列裸题(<不会做,请自裁>系列)
最近BZOJ炸了,而我的博客上又更新了一些基本知识,所以这里刷一些裸题,用以丰富知识性博客 POJ2823 滑动的窗口 这是一道经典的单调队题,我记得我刚学的时候就是用这道题作为单调队列的例题,算 ...
- POJ2823 单调队列
POJ2823 http://poj.org/problem?id=2823 最基础的单调队列,说是数据结构,其实就是一种更新数组数据的方法. 之前还准备用deque,超时了,直接head,tail快 ...
- poj2823单调队列
这个裸题,滑动窗口求最大最小值,单调队列来两边,一次单调递增q[s]就是最小值,一次单调递减q[s]就是最大值 cin会超时,解除同步也没用... #include<map> #inclu ...
- 单调队列(数列中长度不超过k的子序列和的最值)
★实验任务 小 F 很爱打怪,今天因为系统 bug,他提前得知了 n 只怪的出现顺序以及击 倒每只怪得到的成就值 ai.设第一只怪出现的时间为第 1 秒,这个游戏每过 1 秒 钟出现一只新怪且没被击倒 ...
- POJ2823 Sliding Window (单调队列)
POJ2823 Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 38342 Accepte ...
- poj2823:单调队列入门题
今天学习了一下单调队列这种数据结构,思想不是很难 参考资料:http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html 然后自 ...
- poj2823/hdu3415 - 数据结构 单调队列
poj2823 题目链接 长度为N的数组,求宽度k的滑动窗口在数组上滑动时窗口内的最大值或最小值 如果用单调队列做,求最小值时,队列应该严格递增的.所以插入时,队尾大于等于插入值的元素都应被舍弃,因为 ...
- POJ2823 Sliding Window(单调队列)
题目要输出一个序列各个长度k的连续子序列的最大值最小值. 多次RMQ的算法也是能过的,不过单调队列O(n). 这题,队列存元素值以及元素下标,队尾出队维护单调性然后入队,队首出队保持新元素下标与队首元 ...
随机推荐
- linux 二级目录结构
Linux系统里面目录的顶点都是根 /etc /etc/passwd : Linux用户登陆的文件 /etc/group : 存放Linux用户组的文件 /etc/shadow :存放用户密码的文件 ...
- 细数阿里云在使用 Docker 过程中踩过的那些坑
昨天下午道哥在微信上丢给我一条新闻,看看,我们阿里云支持 Docker 企业版了.我打开一看,果然,阿里云发布了飞天敏捷版,开始支持企业级的 Docker 容器. 美国中部时间4月19日,阿里云在容器 ...
- 安装XCode7.1后,QT5.5出现的各种问题解决方案
安装XCode7.1后,突然发现QT5.5编译不了程序了.直接在终端输入clang,竟然输出如下的信息. Agreeing to the Xcode/iOS license requires admi ...
- 关于通过Date.getTime()得到1970年01月1日0点零分问题验证
public static String getTimestamp_1970() throws Exception { java.text.SimpleDateFormat formater = ...
- 【阅读笔记】Ranking Relevance in Yahoo Search (三)—— query rewriting
5. QUERY REWRITING 作用: query rewriting is the task of altering a given query so that it will get bet ...
- 数学--数论--HDU-2698 Maximum Multiple(规律)
Given an integer nn, Chiaki would like to find three positive integers xx, yy and zzsuch that: n=x+y ...
- Codeforce 1255 Round #601 (Div. 2) C. League of Leesins (大模拟)
Bob is an avid fan of the video game "League of Leesins", and today he celebrates as the L ...
- POJ 2136 Vertical Histogram(当时写的比较恶心,优化一下)
Vertical Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21223 Accepted: 10048 ...
- P2765 魔术球问题 网络流二十四题重温
P2765 魔术球问题 知识点::最小点覆盖 这个题目要拆点,这个不是因为每一个球只能用一次,而是因为我们要求最小点覆盖,所以要拆点来写. 思路: 首先拆点,然后就是开始建边,因为建边的条件是要求他们 ...
- Collections集合工具类常用的方法
java.utils.Collections //是集合工具类,用来对集合进行操作.部分方法如下: public static <T> boolean addAll(Collection& ...