Sliding Window

Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 32099   Accepted: 9526
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

一道纯的单调队列,单调队列的经典题:
 #include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
typedef struct abcd
{
int a,i;
}abcd;
typedef struct abcda
{
int b,j;
}abcda;
abcd a[];
abcda b[];
int x[][];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
int h1,h2,t1,t2;
h1=;
h2=;
t1=;
t2=;
int i,z;
for(i=;i<n;i++)
{
scanf("%d",&z);
while(h2<t2&&b[t2-].b>=z) t2--;
b[t2].b=z;
b[t2++].j=i;
while(h1<t1&&a[t1-].a<=z) t1--;
a[t1].a=z;
a[t1++].i=i;
if(i<k-)
continue;
if(i-a[h1].i>=k)h1++;
if(i-b[h2].j>=k)h2++;
x[][i]=a[h1].a;
x[][i]=b[h2].b;
}
for(i=k-;i<n-;i++)
printf("%d ",x[][i]);
printf("%d\n",x[][i]);
for(i=k-;i<n-;i++)
printf("%d ",x[][i]);
printf("%d\n",x[][i]);
}

poj2823一道纯单调队列的更多相关文章

  1. poj2823/hdu3415 - 数据结构 单调队列

    poj2823 题目链接 长度为N的数组,求宽度k的滑动窗口在数组上滑动时窗口内的最大值或最小值 如果用单调队列做,求最小值时,队列应该严格递增的.所以插入时,队尾大于等于插入值的元素都应被舍弃,因为 ...

  2. POJ2823 Sliding Window(单调队列)

    单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. -------------------------------------------------------------- ...

  3. 刷题向》POJ2823 单调队列裸题(<不会做,请自裁>系列)

    最近BZOJ炸了,而我的博客上又更新了一些基本知识,所以这里刷一些裸题,用以丰富知识性博客 POJ2823   滑动的窗口 这是一道经典的单调队题,我记得我刚学的时候就是用这道题作为单调队列的例题,算 ...

  4. POJ2823 Sliding Window (单调队列)

    POJ2823 Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 38342   Accepte ...

  5. poj2823:单调队列入门题

    今天学习了一下单调队列这种数据结构,思想不是很难 参考资料:http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html 然后自 ...

  6. poj2823 单调队列初步

    什么是单调队列:头元素一直是队列当中的最大值,队列中的值按照递减顺序排列,可以从末尾插入一个元素,或从两段删除元素 1.插入元素,为了保证队列的单调性(这里假设为递减性),在插入元素v时要将对位的元素 ...

  7. POJ2823 单调队列

    POJ2823 http://poj.org/problem?id=2823 最基础的单调队列,说是数据结构,其实就是一种更新数组数据的方法. 之前还准备用deque,超时了,直接head,tail快 ...

  8. POJ2823 Sliding Window(单调队列)

    题目要输出一个序列各个长度k的连续子序列的最大值最小值. 多次RMQ的算法也是能过的,不过单调队列O(n). 这题,队列存元素值以及元素下标,队尾出队维护单调性然后入队,队首出队保持新元素下标与队首元 ...

  9. 双端队列(单调队列)poj2823 区间最小值(RMQ也可以)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 41844   Accepted: 12384 ...

随机推荐

  1. Python 的经典入门书籍有哪些?

    是不是很多人跟你说,学Python开发就该老老实实地找书来看,再配合死命敲代码?电脑有了,软件也有了,心也收回来了?万事俱备,唯独只欠书籍?没找到到合适的书籍?可以看看这些. 1.Python基础教程 ...

  2. javaScript手记(01)

    --------------------javaScript基础1.嵌入页面的方式 1.行间事件(主要用于事件): <input type="button" name=&qu ...

  3. Vuejs 页面的区域化与组件封装

    组件的好处 当我用vue写页面的时候,大量的数据页面渲染,引入组件简化主页面的代码量,当代码区域块代码差不多相同时,组件封装会更加简化代码.组件是Vue.js最强大的功能之一. 组件可以扩展HTML元 ...

  4. 我的项目经验总结——CDN镜像:1(初探)

    前言 其实,这个标题有些大,作为一个小白,只是在实际工作中经常听闻我司的CDN服务如何如何牛B……而且我司的云服务还拿到了工信部的CDN牌照……那么,作为一个研发仔,怎么能不去了解和熟悉呢?!不过,这 ...

  5. 使用CXF开发JAX-WS类型的WebService

    使用CXF记得要先加入CXF的jar包 方法1: Cxf编程实现: l 使用jaxwsServerFactoryBean发布 webservice服务端. 需要设置: jaxwsServerFacto ...

  6. Ljava.lang.Object;@ba8a1dc

    因为你从数据库读出数据后,存入到list集合上时,如果你没有指定要存入的数据的类型,系统会自动给你赋一个object类型,他是所以类的鼻祖,你取出数据要进行转型,转化成你自己想要的数据类型才能显示.它 ...

  7. poj 1011--Sticks(搜索)

    题目链接 Description George took sticks of the same length and cut them randomly until all parts became ...

  8. sublime text3 3143 注册码

    sublime text3  3143 注册码,可用~ Sublime Text一款具有代码高亮.语法提示.自动完成且反应快速的编辑器软件,不仅具有华丽的界面,还支持插件扩展机制.相比于难于上手的Vi ...

  9. Swing-JTable的渲染器与编辑器使用demo

    JTable的内容.外观.事件响应在很大程度上是由渲染器与编辑器控制的.具体说来,渲染器负责单元格的外观比如前景色.背景色,以及单元格提示:编辑器负责单元格的内容和事件响应.编辑器默认为文本框形式,也 ...

  10. 201521123012 《Java程序设计》第三周学习总结

    一.本章学习总结 1.面对对象:(1)类 <1>属性 [1]外部类 [2]内部类 <2>方法 [1]j静态 [2]非静态 (2)对象 <1>创建对象 <2&g ...