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

题意:给一个数列,给一个窗口大小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. scss-@while指令

    @while是一个循环指令,其后跟着一个表达式,如果表达式的返回值为false,则停止循环. scss代码实例如下: $i: 6; @while $i > 0 { .item-#{$i} { w ...

  2. Python爬虫《爬取get请求的页面数据》

    一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...

  3. 【代码笔记】Java基础:Java的方法和类

    面向过程与面向对象都是我们编程中,编写程序的一种思维方式.例如:公司打扫卫生(擦玻璃.扫地.拖地.倒垃圾等), 按照面向过程的程序设计方式会思考“打扫卫生我该怎么做,然后一件件的完成”,最后把公司卫生 ...

  4. String字符串操作

    char chars[] ={'a','b','c'}; String s = new String(chars); int len = s.length();//字符串长度 System.out.p ...

  5. 细说CSV

            CSV全称是Comma-Separated Values(逗号分隔值).作为一种数据传输与存储的格式,它显然没有xml,json强大,只能进行一些二维数组的数据处理,但它在项目还是经常 ...

  6. SpannableString与SpannableStringBuilder使用

    转自:http://blog.it985.com/14433.html1.SpannableString.SpannableStringBuilder与String的关系 首先SpannableStr ...

  7. ul标签在FF中默认只有padding值(即:padding-left:40px)

  8. C#发送电子邮件 (异步) z

    ///验证电子邮件的正则表达式   string emailStr = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([ ...

  9. YUV数据详解

    http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html YUV格式有两大类:planar和packed.对于planar的YUV ...

  10. JavaScript 面向对象编程(四)的常用方法、属性总结

    面向对象的属性.方法.操作符总结,都是干货.想深入掌握面向对象的程序设计模式,必须掌握一下知识点.下列知识点注重于实现,原理还请借鉴<javascript高级程序设计> (基于javasc ...