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
解题思路:这题不止一种做法,初学单调队列,这题作为入门题再适合不过了。单调队列维护固定区间长度的最值,求区间长为k中的最大值用单调递减队列,求区间长为k中的最小值用单调递增队列。单调队列和单调栈十分相似,但又有区别。相关讲解:单调队列总结。时间复杂度是O(n)。还有一点这道题要用C++提交,用G++会超时,原因不清楚=_=||。
AC代码(6829ms):
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<deque>
using namespace std;
const int maxn=1e6+;
int n,k,a[maxn],minv[maxn],maxv[maxn];
deque<int> dq1,dq2;
int main(){
while(~scanf("%d%d",&n,&k)){
for(int i=;i<n;++i)scanf("%d",&a[i]);
dq1.clear();dq2.clear();memset(minv,,sizeof(minv));memset(maxv,,sizeof(maxv));
for(int i=;i<n;++i){
while(!dq1.empty()&&a[dq1.back()]>=a[i])dq1.pop_back();//维护窗口最小值,单调递增队列
while(!dq2.empty()&&a[dq2.back()]<=a[i])dq2.pop_back();//维护窗口最大值,单调递减队列
dq1.push_back(i);dq2.push_back(i);
if(i-k+>=){//至少从第k个数开始才有区间最值
minv[i-k+]=a[dq1.front()],maxv[i-k+]=a[dq2.front()];//直接取队首在当前窗口的最值
if(dq1.front()==i-k+)dq1.pop_front();//如果队首下标已经是最大区间(一共k个元素)的左端点值,则将其弹出,窗口向右移动
if(dq2.front()==i-k+)dq2.pop_front();
}
}
for(int i=;i<=n-k;++i)//输出n-k+1个窗口中的最值
printf("%d%c",minv[i],i==n-k?'\n':' ');
for(int i=;i<=n-k;++i)
printf("%d%c",maxv[i],i==n-k?'\n':' ');
}
return ;
}

题解报告:poj 2823 Sliding Window(单调队列)的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  2. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  3. POJ 2823 Sliding Window (单调队列)

    单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...

  4. poj 2823 Sliding Windows (单调队列+输入输出挂)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 73426   Accepted: 20849 ...

  5. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  6. 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

    To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...

  7. POJ 2823 Sliding Window(单调队列 || 线段树)题解

    题意:求每个长度为k的数组的最大值和最小值 思路: 1.用线段树创建维护最大值和最小值,遍历询问,简单复习了一下...有点手生 2.单调队列: 可以看一下详解 单调队列顾名思义就是一个单调递增或者递减 ...

  8. POJ 2823 Sliding Window(单调队列入门题)

      Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 67218   Accepted: 190 ...

  9. POJ 2823 Sliding Window 【单调队列】

    题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗体在这个数组上滑动,要求出每次滑动该窗体内的最大值和最小值. 这就是典型的单调队列,单调队 ...

随机推荐

  1. Enhance Magento 404 page

    Magento default installation already has a predefined custom 404 page (no-route). But is it enough t ...

  2. 初探无线安全审计设备WiFi Pineapple Nano系列之PineAP

    前言: 之前曾经介绍过国外无线安全审计设备The WiFi Pineapple Nano的SSLsplit模块和ettercap模块及实验. 在玩WiFi Pineapple Nano 设备的过程中, ...

  3. BC一周年B

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  4. udhcp源码详解(五) 之DHCP包--options字段

    中间有很长一段时间没有更新udhcp源码详解的博客,主要是源码里的函数太多,不知道要不要一个一个讲下去,要知道讲DHCP的实现理论的话一篇博文也就可以大致的讲完,但实现的源码却要关心很多的问题,比如说 ...

  5. 【iOS系列】-程序开启后台运行

    [iOS系列]-程序开启后台运行 iOS程序是伪后台的运行,可是有时候我们需要让其在后台也要进行一些操作,我们可以让其伪装成音乐的APP,这样就可以让程序后台进行相关操作了,具体做法如下: 1:在Ap ...

  6. 【bzoj2753】[SCOI2012]滑雪与时间胶囊

    #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> # ...

  7. redirect和rewrite

    1 服务器端重定向 客户端想要访问的内容不在该服务器上,该服务器自己去另外的服务器请求到该内容,然后还是由该服务器将内容返回给客户端.称为rewrite. 2 客户端重定向 客户端想要访问的内容不在该 ...

  8. Lightoj 1003 - Drunk(拓扑排序)

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  9. EF1:MVC/EF(Entity Framewok) /First Migrations

    1. 概念 Entity Framework: ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案.(此处 ...

  10. BZOJ2002:Bounce 弹飞绵羊(LCT)

    某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当 ...