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

  1. 8 3
  2. 1 3 -1 -3 5 3 6 7

Sample Output

  1. -1 -3 -3 -3 3 3
  2. 3 3 5 5 6 7

  3. 一道纯的单调队列,单调队列的经典题:
  1. #include <iostream>
  2. #include <string.h>
  3. #include <math.h>
  4. using namespace std;
  5. typedef struct abcd
  6. {
  7. int a,i;
  8. }abcd;
  9. typedef struct abcda
  10. {
  11. int b,j;
  12. }abcda;
  13. abcd a[];
  14. abcda b[];
  15. int x[][];
  16. int main()
  17. {
  18. int n,k;
  19. scanf("%d%d",&n,&k);
  20. int h1,h2,t1,t2;
  21. h1=;
  22. h2=;
  23. t1=;
  24. t2=;
  25. int i,z;
  26. for(i=;i<n;i++)
  27. {
  28. scanf("%d",&z);
  29. while(h2<t2&&b[t2-].b>=z) t2--;
  30. b[t2].b=z;
  31. b[t2++].j=i;
  32. while(h1<t1&&a[t1-].a<=z) t1--;
  33. a[t1].a=z;
  34. a[t1++].i=i;
  35. if(i<k-)
  36. continue;
  37. if(i-a[h1].i>=k)h1++;
  38. if(i-b[h2].j>=k)h2++;
  39. x[][i]=a[h1].a;
  40. x[][i]=b[h2].b;
  41. }
  42. for(i=k-;i<n-;i++)
  43. printf("%d ",x[][i]);
  44. printf("%d\n",x[][i]);
  45. for(i=k-;i<n-;i++)
  46. printf("%d ",x[][i]);
  47. printf("%d\n",x[][i]);
  48. }

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. 基于dijkstra算法求地铁站最短路径以及打印出所有的路径

    拓展dijkstra算法,实现利用vector存储多条路径: #include <iostream> #include <vector> #include <stack& ...

  2. HTML5_input_file_打开很慢的问题

    最近项目中有上传附件的功能,只是在chrome浏览器上面测试,发现上传附件,打开选择框比较慢 原文链接:http://www.foreverpx.cn

  3. Jmeter之Bean shell学习(一)

    一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言(这点和JS类似); BeanS ...

  4. tp下的memcached运用

    来源:http://blog.csdn.net/fudaoji/article/details/50722839   侵删 一.环境: lnmp开发服务器, memcached2.2.0,thinkp ...

  5. Ubuntu Docker Registry 搭建私有仓库

    服务器版本 Ubuntu 16.04 LTS. 安装命令: $ docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --rest ...

  6. memcached常用命令

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt104 一.Memcache面向对象的常用接口包括:Memcache::con ...

  7. java8 去掉 perm 用 Metaspace 来替代

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt266 正如大家所知,JDK 8 Early Access版已经提供下载.这使 ...

  8. 如何实现一个 Virtual DOM 及源码分析

    如何实现一个 Virtual DOM 及源码分析 Virtual DOM算法 web页面有一个对应的DOM树,在传统开发页面时,每次页面需要被更新时,都需要手动操作DOM来进行更新,但是我们知道DOM ...

  9. 一种解决url的get请求参数传值乱码问题的方式

    做项目的时候发现url get请求传中文字符出现乱码问题,百度了一下,最后用一种比较容易理解的方式解决了.分享给大家! 经过百度,网友提到:url get方式提交的参数编码,只支持iso8859-1编 ...

  10. 201521123107 《Java程序设计》第1周学习总结

    第1周学习总结 1.本周学习总结 本周我们正式开始了对一门新的编程语言java的学习.本周的主要内容是初步了解了java的发展过程,java具有简约且简单 .平台无关性等优点.java的3个关键的工具 ...