Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 50738   Accepted: 14590
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
  1. #include"cstdio"
  2. #include"cstring"
  3. #include"queue"
  4. #include"vector"
  5. using namespace std;
  6. const int MAXN=;
  7. int a[MAXN];
  8. int Min[MAXN];
  9. int Max[MAXN];
  10. int n,k;
  11. int cnt;
  12. struct comp1{
  13. bool operator()(const int x,const int y)
  14. {
  15. return a[x] > a[y];
  16. }
  17. };
  18. struct comp2{
  19. bool operator()(const int x,const int y)
  20. {
  21. return a[x] < a[y];
  22. }
  23. };
  24. priority_queue<int,vector<int>,comp1> que1;
  25. priority_queue<int,vector<int>,comp2> que2;
  26. int main()
  27. {
  28. scanf("%d%d",&n,&k);
  29. for(int i=;i<=n;i++)
  30. scanf("%d",&a[i]);
  31. for(int i=;i<=k;i++)
  32. {
  33. que1.push(i);
  34. que2.push(i);
  35. }
  36. Min[cnt]=a[que1.top()];
  37. Max[cnt]=a[que2.top()];
  38. cnt++;
  39. for(int i=k+;i<=n;i++)
  40. {
  41. que1.push(i);
  42. que2.push(i);
  43. while(i-que1.top()>=k) que1.pop();// 将窗口之外的删除
  44. Min[cnt]=a[que1.top()];
  45. while(i-que2.top()>=k) que2.pop();
  46. Max[cnt]=a[que2.top()];
  47. cnt++;
  48. }
  49. for(int i=;i<cnt;i++)
  50. printf("%d%c",Min[i],(i==cnt-)?'\n':' ');
  51. for(int i=;i<cnt;i++)
  52. printf("%d%c",Max[i],(i==cnt-)?'\n':' ');
  53. return ;
  54. }

POJ2823(优先队列)的更多相关文章

  1. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  2. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  3. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  4. 数据结构作业——Sanji(优先队列)

    山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...

  5. Java优先队列

    按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...

  6. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  7. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

  8. Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解

    /* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...

  9. 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)

    ギリギリ eye Description A.D.1999,由坠落地球的“谜之战舰”带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼此间的战争,一方面面 ...

随机推荐

  1. 判断是否是iso8859-1编码

    if (null == keyword || keyword.equals("关键字")) keyword = "";            if(keywor ...

  2. Odoo10尝鲜: 采购协议

    Odoo10 对 call for bid 进行了 改进, 作为 '采购协议' 进入 采购, 选择 'Purchase agreement' 在 agreement 列表 建立新的 协议 选择 协议类 ...

  3. 机器学习(Machine Learning)&amp;深度学习(Deep Learning)资料

    机器学习(Machine Learning)&深度学习(Deep Learning)资料 機器學習.深度學習方面不錯的資料,轉載. 原作:https://github.com/ty4z2008 ...

  4. redis 3.0.1 在CentOS上的安装

    一.下载 wget http://download.redis.io/releases/redis-3.0.1.tar.gz 二.解压 tar xzf redis-3.0.1.tar.gz 三.进入 ...

  5. python(27)- 面向对象练习Ⅰ

    一:定义如下类,并最大程度地重用代码(继承,派生:子类重用父类方法,组合) 老师类 学生类 分数类 课程类 生日类 class People: def __init__(self,name,age,b ...

  6. Ffmpeg 获取USB Camera 视频流

    本文讲述的案例是如何通过Ffmpeg实现从USB Camera中获取视频流并将视频流保存到MP4文件. 本文亦适用于从USB Camera 获取视频流并将视频流转发到rtmp服务的案例,二者基本的原理 ...

  7. Python+Selenium框架 ---一个类文件多个测试方法情况下测试固件的写法

    我们测试中,肯定需要,打开一个页面,然后测试这个页面的多个用例,才关闭这个页面,去测试其他页面,在unittest是有相关测试固件方法去支持这种行为.请看下面 # coding=utf-8 impor ...

  8. Django知识梳理

    请求周期: url > 路由 > 函数或类 > 返回字符串或模板语言 Form 表单提交: 先处理模板语言再讲HTML发出去 提交 > url > 函数或类中的方法  — ...

  9. sql server t-sql脚本转成oracle plsql

    将一份SQL SERVER数据库生成的T-SQL脚本,转成ORACLE的PL/SQL,其复杂繁琐程度,远远出乎我的意料. 这份SQL SERVER脚本,里面有表,有视图,还有存储过程,以及一些自定义函 ...

  10. redis的主从复制和哨兵支持的主从切换

    1 主从复制的目的是为了读写分离 master写,然后同步到slave,slave只管读. 2 哨兵存在的目的 是为了主从切换,如果master挂了,那么一个slave成为master,重启之后的ma ...