POJ 2838 单调队列
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 55309 | Accepted: 15911 | |
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
are two lines in the output. The first line gives the minimum values in
the window at each position, from left to right, respectively. The
second line gives the maximum values.
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
Source
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,k;
int a[];
int ddq[];
void min()
{
int head=,tail=;
for(int i=;i<k-;i++)
{
while(head<=tail&&a[ddq[tail]]>=a[i])
tail--;
tail++;
ddq[tail]=i;
}
for(int i=k-;i<n;i++)
{
while(head<=tail&&a[ddq[tail]]>=a[i])
tail--;
tail++;
ddq[tail]=i;
while(ddq[head]<i-k+)
head++;
printf("%d",a[ddq[head]]);
if(i==n-) printf("\n");
else printf(" "); }
}
void max()
{
int head=,tail=;
for(int i=;i<k-;i++)
{
while(head<=tail&&a[ddq[tail]]<=a[i])
tail--;
tail++;
ddq[tail]=i;
}
for(int i=k-;i<n;i++)
{
while(head<=tail&&a[ddq[tail]]<=a[i])
tail--;
tail++;
ddq[tail]=i;
while(ddq[head]<i-k+)
head++;
printf("%d",a[ddq[head]]);
if(i==n-)
printf("\n");
else printf(" ");
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(ddq,,sizeof(ddq));
memset(a,,sizeof(a));
for(int i=;i<n;i++)
scanf("%d",&a[i]);
min();
max();
}
return ;
}
POJ 2838 单调队列的更多相关文章
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- caioj 1172 poj 2823 单调队列过渡题
给定一个n个数的数列,从左至右输出每个长度为m的数列段内的最大数. 输入:第一行两个整数n和m( 1<= n <= 20 0000,m<=n).下来给出n个整数. 输出:一行一个整数 ...
- poj 3017 单调队列优化动态规划
思路:dp[i]=min{dp[j]+max(num[j+1]...num[i])},其中sum[i]-sum[j]<=m. 那么我们需要用单调队列维护j到i的最大值. #include< ...
- poj 2373 单调队列优化背包
思路:我们用单调队列保存2*b<=i-j<=2*a中的最大值.那么队列头就是最大值,如果队头的标号小于i-2*b的话,就出队,后面的肯定用不到它了. #include<iostrea ...
- poj 2823 单调队列
思路:裸的单调队列. #include<iostream> #include<cstring> #include<cstdio> #include<algor ...
- POJ 3017 单调队列dp
Cut the Sequence Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8764 Accepted: 2576 ...
- POJ 1821 单调队列+dp
题目大意:有K个工人,有n个墙,现在要给墙涂色.然后每个工人坐在Si上,他能刷的最大范围是Li,且必须是一个连续子区间,而且必须过Si,他刷完后能获得Pi钱 思路:定义dp[i][j]表示前i个人,涂 ...
- POJ 2823 单调队列入门水题
最最基础的单调队列题目.一个单增一个单减.还是可以借此好好理解一下单调队列的. #include <stdio.h> #include <string.h> #include ...
- POJ - 1821 单调队列优化DP + 部分笔记
题意:n个墙壁m个粉刷匠,每个墙壁至多能被刷一次,每个粉刷匠要么不刷,要么就粉刷包含第Si块的长度不超过Li的连续墙壁(中间可不刷),每一块被刷的墙壁都可获得Pi的利润,求最大利润 避免重复粉刷: 首 ...
随机推荐
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- Vue#条件渲染
根据不同的条件,响应不同的事件. https://jsfiddle.net/miloer/zed5p1r3/ 可以用template来包装元素,当然浏览器的最终渲染结果不会包含它.我觉得主要用它来自定 ...
- hdu 4826(dp + 记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...
- Android回调接口的写法
方法一: 定义一个接口,里面写想要对外提供的方法,在逻辑层方法的参数里传递进去,让在需要的时候调接口里的方法. 实例一: public class SmsUtils { public interfac ...
- 通俗理解T检验与F检验的区别【转】
转自:http://blog.sina.com.cn/s/blog_4ee13c2c01016div.html1,T检验和F检验的由来一般而言,为了确定从样本(sample)统计结果推论至总体时所犯错 ...
- iOS10 UI教程禁用视图与用户的交互
iOS10 UI教程禁用视图与用户的交互 在上文中我们提到了使用isHidden属性和alpha属性可以使视图与用户的交互被禁用,除此之外此功能还可以使用UIView的isUserInteractio ...
- ajax无刷新异步传输
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- Thymeleaf 常用属性
Thymeleaf 常用属性 如需了解thymeleafThymeleaf 基本表达式,请参考<Thymeleaf 基本表达式>一文 th:action 定义后台控制器路径,类似<f ...
- 5.15[没什么营养的一段日子]A*
五月份没有写过blog. 期中考刚过......漫漫文化课,无尽头. 马上要为联赛开坑了,激动啊. 刚听了孙柘的演讲..%%% 最近刷的题只有一道启发式合并,一道分层图,一道差分约束..然后不知不觉破 ...
- 项目新的需求,网页的自适应交付/响应式交付 Responsive/Adaptive Delivery
网页为什么要做自适应交付,皆因现在移动设备大行其道,现在是移动互联网时代,以IOS及Android为首的各种移动终端已经遍地开花. 当人家用380px的iphone打开你的网页时,你总不能显示个102 ...