【POJ 3784】 Running Median (对顶堆)
Running MedianDescription
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 56Sample 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 -3Source
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 10010 priority_queue<int > q1;
priority_queue<int,vector<int>,greater<int> > q2; int c1,c2,num,n;
int a[Maxn]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
c1=c2=;
while(!q1.empty()) q1.pop();
while(!q2.empty()) q2.pop();
num=;
scanf("%d%d",&num,&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
printf("%d %d\n",num,(n+)/);
for(int i=;i<=n;i++)
{
if(q2.empty()||a[i]<=q2.top()) q1.push(a[i]);
else q2.push(a[i]);
while(q2.size()>=(i+)/) {q1.push(q2.top());q2.pop();}
while(q1.size()>(i+)/) {q2.push(q1.top());q1.pop();}
if(i%==&&i!=n) printf("%d ",q1.top());
else if(i%==) printf("%d",q1.top());
if(i%==) printf("\n");
}printf("\n");
}
return ;
}
2017-01-18 09:18:12
【POJ 3784】 Running Median (对顶堆)的更多相关文章
- POJ 3784.Running Median
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- poj3784 Running Median[对顶堆]
由于我不会讲对顶堆,所以这里直接传上一个巨佬的学习笔记. 对顶堆其实还是很容易理解的,想这题的时候自己猜做法也能把没学过的对顶堆给想出来.后来了解,对顶堆主要还是动态的在线维护集合$K$大值.当然也可 ...
- POJ 3784 Running Median【维护动态中位数】
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- POJ 3784 Running Median(动态维护中位数)
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- POJ 3784 Running Median (动态中位数)
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...
- POJ 3784 Running Median (模拟水过带翻译)
Description Moscow is hosting a major international conference, which is attended by n scientists fr ...
- POJ3784:Running Median
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=3784 用一个"对顶堆& ...
- 【POJ 3784】 Running Median
[题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...
- Running Median POJ - 3784
本题使用对顶堆做法. 为了动态维护中位数,我们可以建立两个堆 :一个大根对,一个小根堆. 用法:在动态维护的过程中,设当前的长度为length,大根堆存从小到大排名 $1 \thicksim \dfr ...
随机推荐
- 【Foreign】Walk [暴力]
Walk Time Limit: 20 Sec Memory Limit: 256 MB Description Input Output Sample Input 3 1 2 3 1 3 9 Sa ...
- Windows Server 2008 R2 SP1安装SQL 2012安装报错之0x858C001B
使用Windows Server 2008 R2 SP1安装SQL 2012的时候总是报这样一个错: SQL Server Setup has encountered the following er ...
- VMware 12安装虚拟机Mac OS X 10.10使用小技巧(虚拟机Mac OS X 10.10时间设置,虚拟机Mac OS X 10.10通过代理上网,Mac OS X 10.10虚拟机优化,VMware虚拟机相互复制)
1:修改Mac OS 系统时间 2:Mac OS系统 通过代理上网 VMware 12安装Mac OS X 10.10虚拟机优化心得 虚拟显卡硬伤,所以必须要优化下才能用,优化的原则就是能精简的精简, ...
- hdu 1217 Arbitrage (spfa算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...
- bzoj 1854 游戏 二分图匹配 || 并查集
题目链接 Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的 ...
- deepin安装metasploit
[1]安装metasploit 1.curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/tem ...
- Python阶段复习 - part 3 - Python函数
利用函数打印9*9乘法表 def cheng(num): for i in range(1,num+1): for j in range(1,i+1): print('{0} * {1} = {2}' ...
- python写一段脚本代码自动完成输入(目录下的所有)文件的数据替换(修改数据和替换数据都是输入的)【转】
转自:http://blog.csdn.net/lixiaojie1012/article/details/23628129 初次尝试python语言,感觉用着真舒服,简单明了,库函数一调用就OK了 ...
- C 实现有追求的线程池 后续
引言 -_- 还是老套路开局 很久以前写过一个有追求的线程池 -> C 实现有追求的线程池 探究 讲述的是一种思路, 并且实现了. 可以一用. 最近在详细搞simplec 框架. 准备发布个正式 ...
- 如何使用vux创建vue项目
1.安装vue-cli,通过vue-v可以查明 安装vue-cli步骤 vue init airyland/vux2 projectPath(项目名字) 2.安装依赖模块 方法1:npm instal ...