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. jquery在ajax新加入的元素后绑定事件click

    使用YII在做一个点击小图.能够在弹出窗体中显示大图的功能的时候,发现.GridView首页面的列表项按点击时一切正常,但按下了下一页后. 再点击小图,就不起作用了.原来,这是GridView使用了a ...

  2. Deepin-安装git

    sudo apt-get install git 命令介绍(安装软件):apt-get install 命令介绍(Debian系列以管理员运行的前缀):sudo

  3. 使用mongostat监视mongodb

    1, 监视一个mongod mongostat 10.80.1.1:27018 1,监视replica set mongostat --host rs0/10.80.1.1:27018,10.80.1 ...

  4. AIX下RAC搭建 Oracle10G(五)安装oracle、建立监听

    AIX下RAC搭建系列 AIX下RAC搭建 Oracle10G(五)安装oracle.建立监听 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 ...

  5. 3.2.1 配置构建Angular应用——简单的笔记存储应用——编辑功能

    本节我们会接着上节课的内容,继续来完成使用Angular来创建简单的笔记存储应用,上一节课,我们完成了笔记的展示功能,本节课,我们来完成编辑功能. 编辑主要是两个功能:编辑现有的笔记以及创建新笔记.首 ...

  6. 2016/05/25 empty() 与 isset()的区别

    对于初学php的人来说,empty()和和isset()用法的区别是很难搞清楚的,他们的用法的差别不仔细去琢磨的话确实很难弄清楚. 先说一下他们的共同点: 都可以判定一个变量是否为空: 都返回bool ...

  7. unix2dos/dos2unix

    dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的(DOS/MAC to UNIX text file format converter).DOS下的文本文件是以\r\n作为断行标志的 ...

  8. 怎样快速刪除Word中超链接?

    有时我们从网上down了一些资料,存到Word文档里,会发现一些文字和图片带有超链接.这其实是Word自动修改功能引起的麻烦,那么,有什么办法可以把这些超链接快速批量删掉吗? 步骤/方法 1 按键盘上 ...

  9. C项目实践--网络协议和套接字编程

    1.TCP/IP协议 TCP/IP协议是一组包括TCP协议和IP协议,UDP(User Datagram Protocol)协议,ICMP(Internet Control Message Proto ...

  10. how to use datatables editor

    Basic initialisation Editor is a Create, Read, Update and Delete (CRUD) extension forDataTables that ...