滑动最小(最大)值,模版题。

题意:给一个数列,给一个窗口大小k,顺序求每个窗口中最大值和最小值。

和挑战中的例题一模一样,就多了一个求最大,改个大于小于符号就行。

算法是利用双端队列:

以求最小值为例,维护这样一个队列:

1.队列中元素保存数列下标,数列中元素(下标)递增,并且下标对应数列中元素(下标对应值)也递增。

显然我们i从0开始遍历保证了队列中保存的下标是递增的,我们只需要设计算法保证下标对应数列中元素也递增即可。

2.加入一个下标时,从后往前删掉所有对应值大于当前下标对应值的下标,使得下标对应元素也能递增。

3.将这个下标加入队列

4.如果队列首位元素在后面不再需要用到了,队列首位后移一位。

举例

n=5

k=3

a={1,3,5,4,2}

加入0 -> {0}

加入1 -> {0,1}

加入2 -> {0,1,2}

当前窗口最小值=a0=1

删除0 -> {1,2}

加入3 -> {1,3} (a2>=a3,删除2)

当前窗口最小值=a1=3

删除1 -> {3}

加入4 -> {4} (a3>=a4,删除3)

当前窗口最小值=a4=2

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

  1. 8 3
  2. 1 3 -1 -3 5 3 6 7

Sample Output

  1. -1 -3 -3 -3 3 3
  2. 3 3 5 5 6 7
  1. #include<iostream>
  2. #include <cstdio>
  3. #define MAX_N 1000000 + 16
  4.  
  5. using namespace std;
  6. int x[MAX_N];
  7. int b[MAX_N],c[MAX_N];
  8. int que[MAX_N];
  9. int l,r;
  10. int main(int argc, char *argv[])
  11. {
  12. int n,k;
  13. cin >> n >> k;
  14. for(int i=; i<n; i++)
  15. cin >> x[i];
  16. l=r=;
  17. for(int i=; i<n; i++)
  18. {
  19. while(l<r&&x[que[r-]]>=x[i] )r--;
  20. que[r++]=i;
  21.  
  22. if(i-k+>=)
  23. {
  24. b[i-k+]=x[que[l]];
  25.  
  26. if(que[l]==i-k+)
  27. {
  28. l++;
  29. }
  30. }
  31. }
  32. l=r=;
  33. for(int i=; i<n; i++)
  34. {
  35. while(l<r&&x[que[r-]]<=x[i] )r--;
  36. que[r++]=i;
  37.  
  38. if(i-k+>=)
  39. {
  40. c[i-k+]=x[que[l]];
  41.  
  42. if(que[l]==i-k+)
  43. {
  44. l++;
  45. }
  46. }
  47. }
  48. for(int i=; i<=n-k; i++)
  49. printf("%d%c",b[i],i==n-k?'\n':' ');
  50. for(int i=; i<=n-k; i++)
  51. printf("%d%c",c[i],i==n-k?'\n':' ');
  52. return ;
  53. }

POJ2823 滑动窗口的更多相关文章

  1. poj2823滑动窗口(单调队列)

    题目传送门 题意:给你一个长度为n的数列,然后用一个长度为k的窗口去框(k<n)每次保存k这个窗口中的最大值和最小值,输出. 思路:这道题最朴素的on2的做法铁定超时,然后我想过一个nlogn的 ...

  2. POJ2823 滑动窗口 (单调队列)

    来学习一下单调队列: 他只可以从队尾入队,但可以从队尾或队首出队,来维护队列的单调性.单调队列有两种单调性:元素的值单调和元素的下标单调. 单调队列可以用来优化DP.状态转移方程形如dp[i]=min ...

  3. poj2823滑动窗口

    这个是单调队列的入门题目.值得注意的一点是队列中的数的index是单调递增的,所以从队首删除的时候从前向后循环找到第一个index满足>= i - k + 1条件的元素作为队首元素就可以了,这也 ...

  4. POJ 2823 滑动窗口 单调队列

    https://vjudge.net/problem/POJ-2823 中文:https://loj.ac/problem/10175 题目 给一个长度为 $N$ 的数组,一个长为 $K$ 的滑动窗体 ...

  5. [LeetCode] Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  6. TCP/IP 协议中的滑动窗口

    一个例子明白发送缓冲区.接受缓冲区.滑动窗口协议之间的关系. 在上面的几篇文章中简单介绍了上述几个概念在TCP网络编程中的关系,也对应了几个基本socket系统调用的几个行为,这里再列举一个例子,由于 ...

  7. Storm Windowing storm滑动窗口简介

    Storm Windowing 简介 Storm可同时处理窗口内的所有tuple.窗口可以从时间或数量上来划分,由如下两个因素决定: 窗口的长度,可以是时间间隔或Tuple数量: 滑动间隔(slidi ...

  8. lintcode 滑动窗口的最大值(双端队列)

    题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为  ...

  9. TCP 三次握手四次挥手, ack 报文的大小.tcp和udp的不同之处、tcp如何保证可靠的、tcp滑动窗口解释

    一.TCP三次握手和四次挥手,ACK报文的大小 首先连接需要三次握手,释放连接需要四次挥手 然后看一下连接的具体请求: [注意]中断连接端可以是Client端,也可以是Server端. [注意] 在T ...

随机推荐

  1. 初识MySQL——人生若如初相逢

    CREATE TABLE `student`(`studentNo` INT (4) NOT NULL PRIMARY KEY COMMENT '学号',`loginPwd` VARCHAR(20) ...

  2. PAT 1062 Talent and Virtue

    #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #i ...

  3. MySQL远程连接:Host 'x' is not allowed to connect to this MySQL server

    远程连接MySQL时发现如下错误: java.sql.SQLException: null, message from server: "Host '192.168.30.23' is no ...

  4. mfc自动创建按钮消息处理并清除

        以前参加一次面试有这道题,当时没有网络没有做出来,今天在网上整理了一下,实现如下. .h中增加下面代码     //生成的消息映射函数     virtual BOOL OnInitDialo ...

  5. Stage4--Python面向对象

    说在前面: Stage1-Stage4简单介绍一下Python语法,Stage5开始用python实现一些实际应用,语法的东西到处可以查看到,学习一门程序语言的最终目的是应用,而不是学习语法,语法本事 ...

  6. Tomcat中部署web应用的三种方式

    Tomcat中部署web应用的三种方式(静态部署)       第一种,针对war或解压后的war,最为常用的是直接操作webapp目录,将完整的war包或者web应用直接放到webapp目录下.使用 ...

  7. Windows 消息框架: SDK教程

    关键字:WindowsSDK 消息机制 http://www.codeproject.com/Articles/599/Windows-Message-Handling-Part-3 Handling ...

  8. JDBC操作数据库的基本步骤:

    JDBC操作数据库的基本步骤: 1)加载(注册)数据库驱动(到JVM). 2)建立(获取)数据库连接. 3)创建(获取)数据库操作对象. 4)定义操作的SQL语句. 5)执行数据库操作. 6)获取并操 ...

  9. CentOS 7.3 下 Mysql(mariadb)的安装

    LNMP的安装中 Nginx的安装很简单,我一般去Nginx官方网站上下载对应版本的rpm包后,上传到终端rpm安装.再此不多赘述. 但是在CentOS7中安装最新的mysql(mariadb)却经常 ...

  10. spring 四种数据源配置方式

    1.spring自带的数据源 DriverManagerDataSource XML代码: <bean id="dataSource" class="org.spr ...