Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 55309   Accepted: 15911
Case Time Limit: 5000MS

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
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

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.

Output

There
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

题意:
N个数一串,一个长度为K的窗口,窗口开始在最左边,每次向右移动一格,问每次窗口内最大值和最小值。
代码:
基本单调队列
 #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 单调队列的更多相关文章

  1. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  2. caioj 1172 poj 2823 单调队列过渡题

    给定一个n个数的数列,从左至右输出每个长度为m的数列段内的最大数. 输入:第一行两个整数n和m( 1<= n <= 20 0000,m<=n).下来给出n个整数. 输出:一行一个整数 ...

  3. poj 3017 单调队列优化动态规划

    思路:dp[i]=min{dp[j]+max(num[j+1]...num[i])},其中sum[i]-sum[j]<=m. 那么我们需要用单调队列维护j到i的最大值. #include< ...

  4. poj 2373 单调队列优化背包

    思路:我们用单调队列保存2*b<=i-j<=2*a中的最大值.那么队列头就是最大值,如果队头的标号小于i-2*b的话,就出队,后面的肯定用不到它了. #include<iostrea ...

  5. poj 2823 单调队列

    思路:裸的单调队列. #include<iostream> #include<cstring> #include<cstdio> #include<algor ...

  6. POJ 3017 单调队列dp

    Cut the Sequence Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8764   Accepted: 2576 ...

  7. POJ 1821 单调队列+dp

    题目大意:有K个工人,有n个墙,现在要给墙涂色.然后每个工人坐在Si上,他能刷的最大范围是Li,且必须是一个连续子区间,而且必须过Si,他刷完后能获得Pi钱 思路:定义dp[i][j]表示前i个人,涂 ...

  8. POJ 2823 单调队列入门水题

    最最基础的单调队列题目.一个单增一个单减.还是可以借此好好理解一下单调队列的. #include <stdio.h> #include <string.h> #include ...

  9. POJ - 1821 单调队列优化DP + 部分笔记

    题意:n个墙壁m个粉刷匠,每个墙壁至多能被刷一次,每个粉刷匠要么不刷,要么就粉刷包含第Si块的长度不超过Li的连续墙壁(中间可不刷),每一块被刷的墙壁都可获得Pi的利润,求最大利润 避免重复粉刷: 首 ...

随机推荐

  1. 用Access作为后台数据库支撑。

    /// <summary> /// 读取Excel文档 /// </summary> /// <param name="Path">文件名称&l ...

  2. sql篇 select from where group by having order by

    以前,自己总是记不住如何用group by,如何用order by,什么时候用group by,什么时候用order by,什么时候两者一起用,怎么用,谁先谁后,现在,我们就一起来说一下Select ...

  3. session应用----登录验证小案例

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  4. 【MongoDB】1.安装--以及简单使用

    第一次接触MongoDB    参考&粘贴:http://jingyan.baidu.com/article/ed15cb1b52b8661be2698162.html 一.安装 1.首先去官 ...

  5. minix3(一)安装以及编辑文件

    作为一条通信狗,最近开始自学操作系统.听说用MINIX3学操作系统很好,就决定跟UCSB的课程试试. 首先在虚拟机上安装MINIX3. 开始用的VM Station,按照百度文库里安装minix3的教 ...

  6. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  7. Liferay 6.2 改造系列之二十四:修改liferay密码的加密方式

    为了便于后期与Cas集成过程中使用数据库用户的方便,将liferay密码的加密方式改为SHA. 在/portal-master/portal-impl/src/portal.properties配置文 ...

  8. java的几种连接池

    连接池的管理用了了享元模式,这里对连接池进行简单设计. 一.设计思路 1.连接池配置属性DBbean:里面存放可以配置的一些属性 2.连接池接口IConnectionPool:里面定义一些基本的获取连 ...

  9. try : finally语句

    try:finally语句不管有没有异常他都会执行:他就是用来清理的try: h=open("ll","r") y=h.read() print (int(y) ...

  10. HTTP基础05--http首部

    HTTP 报文首部 HTTP 请求报文 在请求中,HTTP 报文由方法.URI.HTTP 版本.HTTP 首部字段等部分构成. HTTP 响应报文 在响应中,HTTP 报文由 HTTP 版本.状态码( ...