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

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

Sample Output

1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3

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

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

 #include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> > bh;///从小到大
priority_queue<int,vector<int>,less<int> > sh;///从大到小
int main()
{
int t,n,num;
int i,j,m,p,q,x,y,z,K;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&num,&n);
printf("%d %d\n",num,n/+);
while (!bh.empty())
{
bh.pop();
}
while (!sh.empty())
{
sh.pop();
}
for (i=; i<=n; i++)
{
scanf("%d",&x);
if (sh.empty()||sh.top()>x)
{
sh.push(x);///放入小堆
}
else
{
bh.push(x);///放入大堆
}
if (sh.size()>(i+)/)
{
x=sh.top();
sh.pop();
bh.push(x);
}
if (sh.size()<(i+)/)
{
x=bh.top();
bh.pop();
sh.push(x);
}
if (i%)
{
printf("%d",sh.top());
if (i==n||(i+)%==)
{
printf("\n");
}
else
{
printf(" ");
}
}
}
}
return ;
}

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. BZOJ 2654: tree(二分 最小生成树)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2901  Solved: 1196[Submit][Status][Discuss] Descript ...

  2. 对类Vue的MVVM前端库的实现

    关于实现MVVM,网上实在是太多了,本文为个人总结,结合源码以及一些别人的实现 关于双向绑定 vue 数据劫持 + 订阅 - 发布 ng 脏值检查 backbone.js 订阅-发布(这个没有使用过, ...

  3. npm audit fix

    执行npm install 出现如下提醒   added 253 packages from 162 contributors and audited 1117 packages in 42.157s ...

  4. MongoDB DBA 实践3-----安装mongdb4.0发生错误

    在安装mongodb时,常常会出现一些错误,导致无法安装完全,下面则是各种错误与它们的解决方法: 1.在window机安装mongodb4.0, 其中的一种:由于缺失系统补丁,而导致无法完全安装,具体 ...

  5. iPhone Plus手机的分辨率到底是多少,是1080×1920还是1242×2208?

    近日在准备AppStore上架的时候,需要提供屏幕快照,苹果官方的要求是: 5.5寸的iOS设备的分辨率是:是1080×1920:然而我们如果找一张Plus的屏幕截图,会发现截图的分辨率是1242×2 ...

  6. stm32串口通信实验,一点笔记

    第一次深入学习stm32,花了好长时间才看懂代码(主要是C语言学习不够深入),又花了段时间自己敲了一遍,然后比对教程,了解了利用中断来串口通信的设置方法. 板子是探索版f407,本实验工程把正点原子库 ...

  7. 从零开始一个http服务器(六)-多路复用和压力测试

    从零开始一个http服务器(六)-多路复用和压力测试 代码地址 : https://github.com/flamedancer/cserver git checkout step6 运行: make ...

  8. VIM - 标准模式下简单操作

    1. 概述 标准模式下, 简单操作 移动 删除 复制粘贴 收益 熟练后, 编辑文本基本不需要鼠标操作了 思路 只讲最基本的, 避免初学时的混淆 把基本操作归类了, 方便理解 2. 准备 一篇篇幅较长的 ...

  9. 20155229实验二 《Java面向对象程序设计》实验报告

    20155229实验二 <Java面向对象程序设计>实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 ...

  10. 学号 20155311 2016-2017-2 《Java程序设计》第1周学习总结

    学号 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 JAVA三大平台**:Java SE.Java EE .Java ME. Java SE四个组成部分:J ...