Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 41844   Accepted: 12384
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

双端队列介绍:

deque和vector一样都是标准模板库中的内容,deque是双端队列,在接口上和vector非常相似,在许多操作的地方可以直接替换。假如读者已经能够有效地使用vector容器,下面提供deque的成员函数和操作,进行对比参考。

deque<type>q;

q.empty():判断队列是否为空

q.front()  ,q.back()  队列的首元素和尾元素

q.begin() ,q.end() 返回队列首元素和结尾地址

q.push_front() ,q.push_back() 分别在队首和队尾插入元素

q.pop_front() ,q.pop_back() 删除首元素和尾元素

q.size() 返回容器中元素的个数

q.clear() 清空所有元素

本题最好采用模拟队列,缩短时限,分析怎样维护递增单调队列,开一个结构体要有id序号,和v值两个内容,首先当队列为空的时候,加入第一个元素;对于下一个将要加入的元素,把该元素a的大小和队尾的元素大小tail进行比较,如果a>tail,则移除尾元素,继续比较,直到a<=tail时把a加入到队列的尾部,当k个连续的数列向后移动时,队列中的前面的元素可能已经不再此范围内了,所以还要判断首元素的序号是不是在此时的范围内,如果不在,这删除首元素(即head++)直到满足条件为止,这样便可以维护一个单调递增队列

当维护单调递减序列的道理同上

程序:

#include"cstdio"
#include"cstring"
#include"cstdlib"
#include"cmath"
#include"string"
#include"map"
#include"cstring"
#include"algorithm"
#include"iostream"
#include"set"
#include"queue"
#include"stack"
#define inf 1000000000000
#define M 1000009
#define LL long long
#define eps 1e-12
#define mod 1000000007
#define PI acos(-1.0)
using namespace std;
int a[M],maxi[M],mini[M];
struct node
{
int v,id;
node(){}
node(int id,int v)
{
this->v=v;
this->id=id;
}
}qmin[M*2],qmax[M*2];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=-1)
{
int minhead=0,mintail=0;
int maxhead=0,maxtail=0;
int cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(i<=k)
{
while(mintail>minhead&&a[i]<qmin[mintail-1].v)
{
mintail--;
}
qmin[mintail++]=node(i,a[i]);//把前k个元素入队 while(maxtail>maxhead&&a[i]>qmax[maxtail-1].v)
{
maxtail--;
}
qmax[maxtail++]=node(i,a[i]);
}
}
mini[cnt]=qmin[minhead].v;
maxi[cnt++]=qmax[maxhead].v;
for(int i=k+1;i<=n;i++)
{
while(mintail>minhead&&a[i]<qmin[mintail-1].v)//删除比a[i]小的尾元素
{
mintail--;
}
qmin[mintail++]=node(i,a[i]);
while(mintail>minhead&&i-k>=qmin[minhead].id)//删除不再范围内的首元素
{
minhead++;
} while(maxtail>maxhead&&a[i]>qmax[maxtail-1].v)
{
maxtail--;
}
qmax[maxtail++]=node(i,a[i]);
while(maxtail>maxhead&&i-k>=qmax[maxhead].id)
{
maxhead++;
}
mini[cnt]=qmin[minhead].v;
maxi[cnt++]=qmax[maxhead].v;
}
printf("%d",mini[0]);
for(int i=1;i<cnt;i++)
printf(" %d",mini[i]);
printf("\n%d",maxi[0]);
for(int i=1;i<cnt;i++)
printf(" %d",maxi[i]);
printf("\n");
}
return 0;
}

  

双端队列(单调队列)poj2823 区间最小值(RMQ也可以)的更多相关文章

  1. Java数据结构——用双端链表实现队列

    //================================================= // File Name : LinkQueue_demo //---------------- ...

  2. POJ 2823 Sliding Window + 单调队列

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

  3. 与图论的邂逅01:树的直径&基环树&单调队列

    树的直径 定义:树中最远的两个节点之间的距离被称为树的直径.  怎么求呢?有两种官方的算法(不要问官方指谁我也不晓得): 1.两次搜索.首先任选一个点,从它开始搜索,找到离它最远的节点x.然后从x开始 ...

  4. Sliding Window - 题解【单调队列】

    题面: An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving fr ...

  5. [洛谷P1886]滑动窗口 (单调队列)(线段树)

    ---恢复内容开始--- 这是很好的一道题 题目描述: 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口. 现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的 ...

  6. POJ3162 Walking Race(树形DP+尺取法+单调队列)

    题目大概是给一棵n个结点边带权的树,记结点i到其他结点最远距离为d[i],问d数组构成的这个序列中满足其中最大值与最小值的差不超过m的连续子序列最长是多长. 各个结点到其他结点的最远距离可以用树形DP ...

  7. 51nod 1050 循环数组最大子段和 单调队列优化DP

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 这个呢,这个题之前 求一遍最大值  然后求一遍最小值 ...

  8. Second My Problem First HDU - 3706 单调队列

    单调队列 单调队列是指一个队列内部的元素具有严格单调性的一种数据结构,分为单调递增队列和单调递减队列. 单调队列满足两个性质 1.单调队列必须满足从队头到队尾的严格单调性. 2.排在队列前面的比排在队 ...

  9. P1886 滑动窗口 /【模板】单调队列 方法记录

    原题链接 滑动窗口 /[模板]单调队列 题目描述 有一个长为 \(n\) 的序列 \(a\),以及一个大小为 \(k\) 的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最 ...

随机推荐

  1. by maintaining a log containing a record of each transaction’s activities - The Commit/Rollback Protocol

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Before a transaction is allowed to a ...

  2. 发现了一个很好的Pasical编程软件Lazarus

    下载地址:http://www.lazarus.freepascal.org/ 中文社区:http://www.fpccn.com/ 该软件有几点如下特征: 1.跨平台.体积小(只有100多M) 2. ...

  3. php 通过exec 创建git分支失败

    今天给我们自己的发布系统增加一个新建分支的功能,操作比较简单,但是使用php执行shell命令的时候总是无法push分支到远程,但是登陆服务器执行却是可以的 新建分支命令如下 git fetch -- ...

  4. LINQ延迟查询的例子

    //linq延迟查询.两次查询结果不同 List<string> l = new List<string>() { "aaa", "bbb&quo ...

  5. vim编辑器配置修改

    刚上手的vim,黑底白字,看起来笨死了,于是一顿狂找,终于找到了配置方法. 配置当然要去etc目录下. cd /etc/vim ls -l                        //找到vim ...

  6. Delphi Dll示例

    //MyInt.pas unit MyInt; interface {$IFNDEF MYLIB} function MyAdd(a,b:integer):integer ;stdcall; {$EN ...

  7. 单个未知大小图片在div里面垂直居中的方法。。。添加辅助元素挤一下位置达到居中

    单个未知大小图片在div里面垂直居中的方法...添加辅助元素挤一下位置达到居中   <div class="ServicesLiTopPic"> <i>&l ...

  8. .NET基础加强,找工作之前可以看看这些............

    .NET基础知识加强: 1  变量命名规则:骆驼命名法:第一个字母小写之后的首字母大写,[对于方法名和类名首字母大写]→培养良好的命名规范. 2  构造函数:没有返回值,方法名和类名相同,每个类中都有 ...

  9. 《JAVA NIO》第二章缓冲区

    1.缓冲区的构成 2.缓冲区的类图 3.ByteBuffer ByteBuffer是直接和Channel打交道的,准确的是直接字节缓冲. 问题:直接字节缓冲区和内存映射的关系 4.ByteOrder ...

  10. 打造安全的App!iOS安全系列之 HTTPS

    如何打造一个安全的App?这是每一个移动开发者必须面对的问题.在移动App开发领域,开发工程师对于安全方面的考虑普遍比较欠缺,而由于iOS平台的封闭性,遭遇到的安全问题相比于Android来说要少得多 ...