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. crm开发(基于ssh)(2)

    今天内容 1 新增客户 2 客户列表 3 修改客户 4 删除客户 5 分页显示客户列表 Hibernate模板里面的方法 1 新增客户 配置 <mapping resource="cn ...

  2. Java中使用Log的方法

    一.java自带log:java.util.logging.Logger使用三步曲 public class HelloLogWorld { private static String name = ...

  3. saltstack学习篇

    参考链接:http://sofar.blog.51cto.com/353572/1596960/ http://sofar.blog.51cto.com/353572/1596960/ 自动化运维工具 ...

  4. Redis 命令,键(key),字符串(String),哈希(Hash),列表(List),集合(Set)(二)

      Redis 命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端.Redis 客户端在我们之前下载的的 redis 的安装包中. ...

  5. JQuery直接调用asp.net后台WebMethod方法(转)

    转自  http://blog.csdn.net/handsometone1982/article/details/7684894 利用JQuery的$.ajax()可以很方便的调用asp.net的后 ...

  6. main(argc,argv[])

    每个C语言程序都必须有一个称为main()的函数,作为程序启动的起点.当执行程序时,命令行参数(command-line argument)(由shell逐一解析)通过两个入参提供给main()函数. ...

  7. ubuntu android 开始git安装

    ubuntu android 开始git安装   git安装: http://source.android.com/source/initializing.html网站提示到以下网址下载: http: ...

  8. React Native探索(五)使用fetch进行网络请求

    相关文章 React Native探索系列 前言 React Native可以使用多种方式来请求网络,比如fetch.XMLHttpRequest以及基于它们封装的框架,fetch可以说是替代XMLH ...

  9. JMX心得 -- Server端

    关于什么是JMX,这里引用了网上找来的一个介绍:JMX(JavaManagement Extensions)是一个为应用程序植入管理功能的框架.JMX是一套标准的代理和服务,实际上,用户可以在任何Ja ...

  10. 给Java新手的一些建议——Java知识点归纳(Java基础部分)

    写这篇文章的目的是想总结一下自己这么多年来使用java的一些心得体会,主要是和一些java基础知识点相关的,所以也希望能分享给刚刚入门的Java程序员和打算入Java开发这个行当的准新手们,希望可以给 ...