【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 ...
随机推荐
- 数据分析之CE找数据大法
一.基本介绍 CE的全称为Cheat Engine,是一款内存修改编辑工具,其官网是http://www.cheatengine.org,可以在其官网下载到最新的CE工具,目前最新版本是Cheat E ...
- 【HNOI】 lct tree-dp
[题目描述]给定2-3颗树,每个边的边权为1,解决以下独立的问题. 现在通过连接若干遍使得图为连通图,并且Σdis(x,y)最大,x,y只算一次. 每个点为黑点或者白点,现在需要删除一些边,使得图中的 ...
- 【Python学习笔记】Coursera之PY4E学习笔记——String
1.字符串合并 用“+”来进行字符串的合并,注意空格是要自己加的. 例: >>> a='Hello' >>> b= a+ 'There' >>> ...
- defconfig file 的 位置
Platform MSM8917 MSM8937 defconfig file position Android/kernel/msm-3.18/arch/arm/configs/
- 简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环)
简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环) http://www.cnblogs.com/M18-BlankBox/p/5881700.html
- python string 对齐文本的几个方法
用rjust().ljust()和center()方法对齐文本
- 【LabVIEW技巧】LabVIEW OOP怎么学
前言 有很多人对LabVIEW OOP存在比较极端的看法,大致分为两类: 1. 绝对否定派认为LabVIEW OOP只不过是LabVIEW为了追求时髦,在面向过程的基础上用簇做了一些特性,实际上完全不 ...
- Android SDK更新 Connection to http://dl-ssl.google.com refused
问题: Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xml, reason: Conne ...
- ie6、ie7下overflow失效
如果父对象有overflow:hidden属性,子对象中的position属性是relative或者absolute, 那么在ie6和ie7下父对象的overflow会失效,解决办法是给父对象加rel ...
- jmeter压力测试案例实战
1. 测试目标地址:http://www.cnblogs.com/ 2. 1秒内有100个用户同时访问,看性能如何 3. 步骤 线程组.http请求.查看结果树.聚合报告 添加http请求如下: ...