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

题意:给一个数列,给一个窗口大小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

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<iostream>
#include <cstdio>
#define MAX_N 1000000 + 16 using namespace std;
int x[MAX_N];
int b[MAX_N],c[MAX_N];
int que[MAX_N];
int l,r;
int main(int argc, char *argv[])
{
int n,k;
cin >> n >> k;
for(int i=; i<n; i++)
cin >> x[i];
l=r=;
for(int i=; i<n; i++)
{
while(l<r&&x[que[r-]]>=x[i] )r--;
que[r++]=i; if(i-k+>=)
{
b[i-k+]=x[que[l]]; if(que[l]==i-k+)
{
l++;
}
}
}
l=r=;
for(int i=; i<n; i++)
{
while(l<r&&x[que[r-]]<=x[i] )r--;
que[r++]=i; if(i-k+>=)
{
c[i-k+]=x[que[l]]; if(que[l]==i-k+)
{
l++;
}
}
}
for(int i=; i<=n-k; i++)
printf("%d%c",b[i],i==n-k?'\n':' ');
for(int i=; i<=n-k; i++)
printf("%d%c",c[i],i==n-k?'\n':' ');
return ;
}

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. 课堂笔记&总结与遇错纠错篇

    一.课堂笔记 二.个人总结 在学习和工作JDK是必不可少的程序员必备工具,遇到问题可以在帮助文档寻找答案! 接受能力不足,老师讲的知识点过去了,我经常还在想上一个知识点.希望老师有时候重点可以讲慢点哈 ...

  2. Java ScheduledExecutorService源码分析

    Java 定时任务可以用Timer + TimerTask来做,或者使用ScheduledExecutorService,使用ScheduledExecutorService有两个好处: 1. 如果任 ...

  3. 微服务学习笔记二:Eureka服务注册发现

    Eureka服务注册发现 服务发现:云端负载均衡,一个基于 REST 的服务,用于定位服务,以实现云端的负载均衡和中间层服务器的故障转移. 1. Service Discovery: Eureka S ...

  4. sass(混合mixin的调用、@content)

    sass中使用@mixin声明混合,可以传递参数,参数名以$符号开始,多个参数以逗号分开,也可以给参数设置默认值.声明的@mixin通过@include来调用 1.无参数mixin scss.styl ...

  5. contentType和dataType

    contentType: 告诉服务器,我要发什么类型的数据 dataType:告诉服务器,我要想什么类型的数据,如果没有指定,那么会自动推断是返回 XML,还是JSON,还是script,还是Stri ...

  6. IP:192.168.21.173 子网掩码:255.255.255.0 网关:192.168.21.2 DNS:8.8.8.8 8.8.4.4 1、设置IP地址、网关 ee /etc/rc.conf #编辑 ifconfig_em0="inet 192.168.21.173 netmask 255

    IP:192.168.21.173子网掩码:255.255.255.0网关:192.168.21.2DNS:8.8.8.88.8.4.41.设置IP地址.网关ee /etc/rc.conf #编辑if ...

  7. 导致SharePoint发生Timeout的几处门槛设置

    IIS connection time-out setting =========================== 如何修改? Click Start, point to All Programs ...

  8. 腾讯云微信小程序域名变更指南

    1 . 将域名添加到云解析里面, 将解析的地址指向已有的小程序负载均衡地址: https://console.qcloud.com/cns 将域名添加到解析列表 添加成功之后,点击解析按钮,添加二级域 ...

  9. 字段处理rtrim去掉结尾的特殊字符和空格

    SQL> create table ycrtest2 (name varchar2(50)); Table created. SQL> insert into ycrtest2 value ...

  10. 模拟误删除InnoDB ibdata数据文件恢复

    注意:假如误删除 ibdata文件 ,此时千万别把mysqld进程杀死,否则没法挽救. 1.模拟删除ibdata数据文件和重做日志文件: [root@hcdb0 data]# lltotal 4219 ...