Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 67218   Accepted: 19088
Case Time Limit: 5000MS

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

Source

这个问题用RMQ或者倍增法能在o(nlogn)内解决。 但是用双端队列(单调队列)能在o(n)内解决。 求最小值:建立一个单调递增队列,元素从左到右依次入队,入队之前必须从队列发问开始删除那些比当前入队元素大或者相等的元素,直到遇到一个比当前入队元素小的元素,或者队列为空为止。若此时队列的大小超过窗口值,则从队头删除元素,直到队列大小小入窗口值为止。然后把当前元素插入队尾。

每滑动一次,取队列的队首元素作为这一区间范围的最小值。并把超过区间范围的最小值删除。若有更小的值入队,则把队列里其它比它大的删了,因为这个最小值在它们后面,又比它们小,所以它们没有存在的意义了。

 
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstdio>
#include<time.h>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int mi[];
int ma[];
int a[];
deque<int>q;
int main()
{
int n,k;
scanf("%d %d",&n,&k);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
while(!q.empty ()) q.pop_back();
for(int i=;i<=n;i++)
{
while(!q.empty ()&&a[q.back ()]>=a[i]) q.pop_back();
if(q.empty ()) mi[i]=i;
else mi[i]=q.front ();
q.push_back (i);
if(q.front ()==i-k+) q.pop_front ();
} while(!q.empty ()) q.pop_back();
for(int i=;i<=n;i++)
{
while(!q.empty ()&&a[q.back ()]<=a[i]) q.pop_back();
if(q.empty ()) ma[i]=i;
else ma[i]=q.front ();
q.push_back (i);
if(q.front ()==i-k+) q.pop_front ();
} for(int i=k;i<=n;i++)
{
printf("%d",a[mi[i]]);
if(i!=n) printf(" ");
}
printf("\n");
for(int i=k;i<=n;i++)
{
printf("%d",a[ma[i]]);
if(i!=n) printf(" ");
}
return ;
}

POJ 2823 Sliding Window(单调队列入门题)的更多相关文章

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

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

  2. POJ 2823 Sliding Window + 单调队列

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

  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 UESTCoj 1221 Sliding Window 单调队列 经典入门题

    题意:给出一个序列,求出每连续k个数字中最大的数和最小的数. 这是道单调队列裸题,直接写就行了. 本来用deque写出来后,发现在poj上硬是超时了,在discuss上看很多人也在抱怨超时的问题,据说 ...

  8. POJ 2823 Sliding Window​ (模板题)【单调队列】

    <题目链接> <转载于>>> > 题目大意: 给你一段序列和一个长为k的窗口,这个窗口从最左边逐渐向右滑,直到滑到最右边,问你,该窗口在滑动的过程中,最大值和 ...

  9. 题解报告:poj 2823 Sliding Window(单调队列)

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

随机推荐

  1. mybatis引入dtd约束

    window->preferences,然后寻找xml catalog,点击add如下所示 将dtd网址复制到key中 key type选择uri,选择dtd的下载路径.

  2. angularjs地址栏传参

    1:路由定义参数 2.controller 3. 4.目标得到参数值

  3. ps(笔记)

    窗口 工作区 默认窗口(恢复) ctrl + n 点阵图(像素图) 小方格组成的 alt 键 配合 放大缩小 ppi dpi 打印输出. 画布新建 z键 局部放大 右击实际像素操作 f键 全屏 空格键 ...

  4. javaweb图片上传 tomcat重新部署 图片消失

      标签: 图片上传tomcat重新部署图片消失原因分析以及解决办法 最近在做一个Javaweb的项目,涉及到图片上传,并且需要将图片通过URL回显给JSP页面,在调试的时候发现,上传到tomcat的 ...

  5. 使用ZooKeeper实现Java跨JVM的分布式锁(优化构思)

    一.使用ZooKeeper实现Java跨JVM的分布式锁 二.使用ZooKeeper实现Java跨JVM的分布式锁(优化构思) 三.使用ZooKeeper实现Java跨JVM的分布式锁(读写锁) 说明 ...

  6. 【spark】IDEA建立基于scala语言的spark项目

    1.新建一个Spark项目 2.选择maven,用模板创建项目 如果 没有这个模板,我们需要添加一个 我们这里使用的是1.6版本Archetype Group Id : net.alchim31.ma ...

  7. SPI笔记

    sclk(serial clock):串行时钟 MOSI(master out slave input)  (master   主机) (slave 从机) MISO(master int slave ...

  8. 条款25:考虑写出一个不抛出异常的swap函数

    首先说下标准库的swap算法: namespace std{ template<typename T> void swap(T & a, T & b) { T tmp = ...

  9. react-webpack(二)

    const path = require('path'); const webpack = require('webpack'); const HtmlWebpackPlugin = require( ...

  10. 《深入理解java虚拟机》学习笔记之虚拟机即时编译详解

    郑重声明:本片博客是学习<深入理解java虚拟机>一书所记录的笔记,内容基本为书中知识. Java程序最初是通过解释器(Interpreter)进行解释执行的,当虚拟机发现某个方法或代码块 ...