Description

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

题目意思:对于n个数,每输入到奇数个数,便求一次中位数。

解题思路:我开始就直接暴力,到了奇数位,sort一下,找出中位数,这样在UVAlive上没有超时,但在poj上是超时的,看了看网上的代码才知道处理这样一个动态的中位数使用堆来维护,而这个堆使用优先队列来模拟,一周内第二次见到优先队列了。维护一个大根堆和一个小根堆,且保证大根堆里的所有数都比小根堆里的所有数小,而且大根堆的大小等于小根堆或者大1,则大根堆堆顶就是中位数。对于新插入的数,与中位数比较决定插入哪个堆中,插入之后维护一下两个堆的大小。每次维护需要进行常数个堆上的操作,所以复杂度O(nlogn)。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5. priority_queue<int,vector<int>,greater<int> > bh;///从小到大
  6. priority_queue<int,vector<int>,less<int> > sh;///从大到小
  7. int main()
  8. {
  9. int t,n,num;
  10. int i,j,m,p,q,x,y,z,K;
  11. scanf("%d",&t);
  12. while (t--)
  13. {
  14. scanf("%d%d",&num,&n);
  15. printf("%d %d\n",num,n/+);
  16. while (!bh.empty())
  17. {
  18. bh.pop();
  19. }
  20. while (!sh.empty())
  21. {
  22. sh.pop();
  23. }
  24. for (i=; i<=n; i++)
  25. {
  26. scanf("%d",&x);
  27. if (sh.empty()||sh.top()>x)
  28. {
  29. sh.push(x);///放入小堆
  30. }
  31. else
  32. {
  33. bh.push(x);///放入大堆
  34. }
  35. if (sh.size()>(i+)/)
  36. {
  37. x=sh.top();
  38. sh.pop();
  39. bh.push(x);
  40. }
  41. if (sh.size()<(i+)/)
  42. {
  43. x=bh.top();
  44. bh.pop();
  45. sh.push(x);
  46. }
  47. if (i%)
  48. {
  49. printf("%d",sh.top());
  50. if (i==n||(i+)%==)
  51. {
  52. printf("\n");
  53. }
  54. else
  55. {
  56. printf(" ");
  57. }
  58. }
  59. }
  60. }
  61. return ;
  62. }

POJ 3784 Running Median(动态维护中位数)的更多相关文章

  1. POJ 3784 Running Median【维护动态中位数】

    Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...

  2. POJ 3784 Running Median (动态中位数)

    题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...

  3. POJ 3784.Running Median

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

  4. POJ 3784 Running Median (模拟水过带翻译)

    Description Moscow is hosting a major international conference, which is attended by n scientists fr ...

  5. HDU 3282 Running Median 动态中位数,可惜数据范围太小

    Running Median Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  6. 【POJ 3784】 Running Median

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

  7. Running Median POJ - 3784

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

  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. MyEclipse报错:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException Communications link failure

    数据库服务没有开或者是驱动那块的问题

  2. 字符编码——python学习

    python学习—字符编码 例如汉字“中” 十进制:20013 二进制:01001110 00101101(unicode)/11100100 10111000 10101101(utf-8) 十六进 ...

  3. keil5 配置 stm32f103rc 软件仿真

  4. java int 与 Integer之间的区别

    int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...

  5. js判断两个日期是否相等的方法

    今天优化代码的时候,发现一个问题,js比较日期是否相等时,我用==去比较,发现两个时间不相等但是运行结果却是true,然后去百度了下发现oldStartTime, startTime都是对象,类型为引 ...

  6. 20155212 2016-2017-2 《Java程序设计》第1周学习总结

    20155212 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 Chapter 1 Java平台概论 Java一开始就是为了有着有限内存与运算资源的消费型数 ...

  7. 20155227 《Java程序设计》实验五 Java网络编程及安全实验报告

    20155227 <Java程序设计>实验五 Java网络编程及安全实验报告 实验内容 任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能. 编写MyDC.java实现从上面 ...

  8. 20155235 2016-2017-2 《Java程序设计》第9周学习总结

    20155235 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 JDBC入门 JDBC简介 连接数据库 使用Statement.Res ...

  9. 关于web服务安全的一些思考

    一.问题: 在开发web项目是时,安全问题有以下几种问题: (1)用户可以自己伪造一个URL请求来进行访问吗? (2)用户不在服务器登录,可以自己封装出用户名.密码进行访问吗? (3)url的参数可以 ...

  10. 让div跟着鼠标移动

    朋友求助帖 具体实现代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...