For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer M, (1 ≤ M ≤ 9999), giving the total number of signed integers to be processed. The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values.

Output

For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the output.

Sample Input

  1. 3
  2. 1 9
  3. 1 2 3 4 5 6 7 8 9
  4. 2 9
  5. 9 8 7 6 5 4 3 2 1
  6. 3 23
  7. 23 41 13 22 -3 24 -31 -11 -8 -7
  8. 3 5 103 211 -311 -45 -67 -73 -81 -99
  9. -33 24 56

Sample Output

  1. 1 5
  2. 1 2 3 4 5
  3. 2 5
  4. 9 8 7 6 5
  5. 3 12
  6. 23 23 22 22 13 3 5 5 3 -3
  7. -7 -3
  8.  
  9. 题意:每组M个数,然后对于每组数读入的时候,只要读入了奇数个的数,就求出先前读入数的中位数,然后输出
  10.  
  11. 思路:可以采用两个优先队列的做法,如果当前读入的数>当前中位数,插入小根堆,否则插入大根堆,这样实际上就维护了中位数相邻两侧的值。
    然后维护 num【小根堆】 - num【大根堆】 <= 1,就是维护中位数两侧的数量应当均分,这样小根堆的top,即是中位数
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<queue>
  4. using namespace std;
  5.  
  6. int t;
  7. int cas,n;
  8. const int maxn = 1e4+;
  9. int ans[maxn];
  10. int main()
  11. {
  12. scanf("%d",&t);
  13. while(t--)
  14. {
  15. priority_queue<int,vector<int>,greater<int> >que1;
  16. priority_queue<int,vector<int>,less<int> >que2;
  17.  
  18. scanf("%d%d",&cas,&n);
  19. printf("%d %d\n",cas,n/+);
  20. int tmp;
  21. int l=,r=,num=;
  22. int cnt = ;
  23. for(int i=;i<=n;i++)
  24. {
  25. scanf("%d",&tmp);
  26. if(tmp > num)
  27. {
  28. que1.push(tmp);
  29. r++;
  30. }
  31. else
  32. {
  33. que2.push(tmp);
  34. l++;
  35. }
  36. if(r < l)
  37. {
  38. int f = que2.top();
  39. que2.pop();
  40. que1.push(f);
  41. r++,l--;
  42. }
  43. else if(r > l + )
  44. {
  45. r--,l++;
  46. int f = que1.top();
  47. que1.pop();
  48. que2.push(f);
  49. }
  50. num = que1.top();
  51. if(i&)
  52. {
  53. ans[++cnt] = num;
  54. }
  55. }
  56. for(int i=;i<=cnt;i++)
  57. {
  58. printf("%d",ans[i]);
  59. if(i != cnt && i%!=)printf(" ");
  60. else printf("\n");
  61. }
  62. }
  63. }
  1. 链表:
    链表主要是个离线处理,先将所有的数据读入,然后记录下其下标。
    然后对其排序,记录下在有序序列下,原来下标的数在哪出现。
    然后从n~1进行处理,因为n永远是该序列剩余的数中最后出现的,也就是说不会将该数后未输入的数进行计算。
  2.  
  3. (关于中位数下标pos的移动那,按照写法是有问题的,但是这题上适用)
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. int t;
  7. int cas,n;
  8. const int maxn = 1e4+;
  9. struct Node
  10. {
  11. int val;
  12. int next;
  13. int pre;
  14. int index;
  15. }node[maxn];
  16. int p[maxn];
  17.  
  18. bool cmp(Node a,Node b)
  19. {
  20. return a.val < b.val;
  21. }
  22. int ans[maxn];
  23. int main()
  24. {
  25. scanf("%d",&t);
  26. while(t--)
  27. {
  28. scanf("%d%d",&cas,&n);
  29. printf("%d %d\n",cas,(n+)>>);
  30. for(int i=;i<=n;i++)
  31. {
  32. scanf("%d",&node[i].val);
  33. node[i].index = i;
  34. }
  35. sort(node+,node++n,cmp);
  36. for(int i=;i<=n;i++)
  37. {
  38. node[i].next = i+;
  39. node[i].pre = i-;
  40. }
  41. node[].pre = node[n].next = -;
  42. for(int i=;i<=n;i++)
  43. {
  44. p[node[i].index] = i;
  45. }
  46. int pos = (n+)>>;
  47. int tot = ;
  48. int l=,r=;
  49. for(int i=n;i>;i--)
  50. {
  51.  
  52. if(i & )
  53. {
  54. if(l > r)pos = node[pos].next;
  55. else if(l < r)pos = node[pos].pre;
  56. ans[++tot] = node[pos].val;
  57. l=r=;
  58. }
  59. if(p[i] >= pos)r++;
  60. if(p[i] <= pos)l++;
  61. if(node[p[i]].pre != -)
  62. {
  63. node[node[p[i]].pre].next = node[p[i]].next;
  64. }
  65. if(node[p[i]].next != -)
  66. {
  67. node[node[p[i]].next].pre = node[p[i]].pre;
  68. }
  69. }
  70. for(int i=;i<=tot;i++)
  71. {
  72. printf("%d",ans[tot-i+]);
  73. if(i% != && i != tot)printf(" ");
  74. else printf("\n");
  75. }
  76. }
  77. }
  1.  

Running Median POJ - 3784 (对顶堆/优先队列 | 链表)的更多相关文章

  1. Running Median POJ - 3784

    本题使用对顶堆做法. 为了动态维护中位数,我们可以建立两个堆 :一个大根对,一个小根堆. 用法:在动态维护的过程中,设当前的长度为length,大根堆存从小到大排名 $1 \thicksim \dfr ...

  2. POJ 2442 Sequence堆 优先队列

    题目描述 给定m个序列,每个序列包含n个非负整数.现在我们可以从每个序列中选择一个数字以形成一个具有m个整数的序列.显然,我们可以得到n ^ m种这种序列.然后,我们可以计算每个序列中的数字总和,并获 ...

  3. POJ 3784.Running Median

    2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...

  4. 【POJ 3784】 Running Median (对顶堆)

    Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...

  5. 【POJ 3784】 Running Median

    [题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...

  6. poj3784 Running Median[对顶堆]

    由于我不会讲对顶堆,所以这里直接传上一个巨佬的学习笔记. 对顶堆其实还是很容易理解的,想这题的时候自己猜做法也能把没学过的对顶堆给想出来.后来了解,对顶堆主要还是动态的在线维护集合$K$大值.当然也可 ...

  7. hdu3282 链表或者对顶堆

    维护序列的动态中位数 第一次用链表做题..感觉指针指来指去也挺麻烦的.. 本题链表解法就是用数组模拟出一个链表,然后离线输入所有数,排序,按照输入顺序在链表里删除元素,一次性删掉两个,然后中位数指针对 ...

  8. POJ3784:Running Median

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=3784 用一个"对顶堆& ...

  9. 【POJ3784】Running Median

    Running Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3406   Accepted: 1576 De ...

随机推荐

  1. 如果Android真的收费了,你怎么看?

    前言 今天突然看到一群里有人发了下面这样一张图片,然后群里又炸了!   于是又和同事讨论了android收费的问题,然后隔壁正在玩农药的UI妹子就笑了... 没错! 安卓可能要收费了!安卓可能要收费了 ...

  2. VM_Centos7.3_X64_安装Oracle12C 总结笔记

    声明:本文居多内容参考原文来之网络: 一:安装Centos7.3 虚拟机 1:操作系统下载 CentOS 7官方下载地址:https://www.centos.org/download/ 说明:本案例 ...

  3. Confluence 6 从站点首页集中访问面板

    如果你选择设置一个页面为你的站点主页面,但是你还是希望你的用户能够访问 Confluence 的主面板,你可以将主面板的连接添加到应用导航(Application Navigator)中. 希望添加 ...

  4. IPv4和IPv6简单对比介绍(转载)

    原链接:https://baijiahao.baidu.com/s?id=1570208896149974&wfr=spider&for=pc 在配置计算机网络,特别是内网的时候,有时 ...

  5. Android 框架 Afinal使用

    介绍android Afinal框架功能: Afinal是一个开源的android的orm和ioc应用开发框架.在android应用开发中,通过Afinal的ioc框架,诸如UI绑定,事件绑定,通过注 ...

  6. 整合Flask中的目录结构

    一.SQLAlchemy-Utils 由于sqlalchemy中没有提供choice方法,所以借助SQLAlchemy-Utils组件提供的choice方法 import datetime from ...

  7. 《剑指offer》 包含min函数的栈

    本题来自<剑指offer> 包含min函数的栈 题目: 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 思路: 举例子让抽象问题具体 ...

  8. python(7):sympy模块

    sympy主要用于符号计算 1,基本操作 from sympy import* #from sympy import pprint #x=Symbol('x')#也可以这么单个定义 #y=Symbol ...

  9. ps和AI使用过程中的易错点整理

    ps:1.视图工具:1)标尺2)参考线3)网格:视图-->--显示>-->网格4)修改网格:编辑-->首选项>-->参考线.网格和切片 5)放大工具:画布中单击可放 ...

  10. swagger2访问url

    swagger : http://localhost:8080/swagger/index.html springboot中的swagger:http://localhost:8080/swagger ...