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

题意:给一个数列,给一个窗口大小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. XML深入了解(XML JavaSprint)

    XMLHttpRequest 对象 XMLHttpRequest 对象用于在后台与服务器交换数据. XMLHttpRequest 对象是开发者的梦想,因为您能够: 在不重新加载页面的情况下更新网页 在 ...

  2. js之箭头函数

    原文 ES6标准新增了一种新的函数:Arrow Function(箭头函数). 为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: ...

  3. C# 用tabcontrol实现窗体类似网页排版的显示

    这里做的比较简陋,可以美化下 把form设置为非顶级控件,直接放在tabcontrol里边,然后实现tabcontrol的拖拽移除tabpage显示form以及添加tabpage mousemove的 ...

  4. Java的API及Object

    API: Java API就是JDK中提供给我们使用的类,这些类将底层的代码实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可. 源文件使用方法: Object类概述: O ...

  5. TopcoderSRM679 Div1 250 FiringEmployees(树形dp)

    题意 [题目链接]这怎么发链接啊..... 有一个 \(n\) 个点的树,每个点有点权(点权可能为负) ,求包含点\(1\)的最 大权连通子图(的权值和) . \(n \leqslant 2500\) ...

  6. 讲座: conversation

    一, Zhouming MSRA NLP group NLP 2.0 attention model 二,Yan Rui 一, retrived based-conversation system t ...

  7. show_sql和format_sql

    <property name="show_sql">true</property> <property name="hibernate.fo ...

  8. java实现12306的45分钟内支付,45分钟后取消订单功能?

    java实现12306的45分钟内支付,45分钟后取消订单功能? - 回答作者: 匿名用户 https://zhihu.com/question/27254071/answer/35948645

  9. spring boot拦截器配置

    1.在spring boot配置文件application.properties中添加要拦截的链接 com.url.interceptor=/user/test 2.编写拦截器代码 ,创建UrlInt ...

  10. npm 安装axios和使用增删改查

    1:安装axios(建议安装淘宝镜像) npm install axios 2:项目导入 npm install --save axios vue-axios 3:页面导入 import axios ...