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. jquery file upload使用

    <!-- jquery file upload相关js --> <script src="/js/jquery-file-upload/js/jquery.ui.widge ...

  2. css动画Demo---水波动画和边框动画

    先上效果图: 水波动画: 边框动画: 1.水波动画 实现代码 <!DOCTYPE html> <html lang="en"> <head> & ...

  3. 课时14.DTD文档声明上(掌握)

    1.什么是DTD文档声明? 由于HTML有很多格版本的规范,每个版本的规范之间又又一些差异,所以为了让浏览器能够正确的编译/解析/渲染我们的网页,我们需要在HTML文件的第一行告诉浏览器,我们当前这个 ...

  4. 虚拟机VMware安装linux无法上网解决办法

    虚拟机VMware安装linux无法上网解决办法   Linux网络设置: 依次单击[System]-->[Preferences]-->[Network Connections],如下图 ...

  5. Delphi调用爷爷类的方法

    Delphi通过inherited 可以调用父类的方法,但是没有提供直接调用父类的父类的方法(爷爷类),通过变通的方式实现如下: 假设父类是TFather,爷爷类TGrand,调用爷爷类的Write方 ...

  6. md5.digest()与md5.hexdigest()之间的区别及转换

    举给例子 md5 = hashlib.md5('adsf') md5.digest() //返回: '\x05\xc1*(s48l\x94\x13\x1a\xb8\xaa\x00\xd0\x8a' # ...

  7. anaconda创建python虚拟环境

    参考: 1.https://blog.csdn.net/lyy14011305/article/details/59500819 我的安装过程: 1. 之前已经安装了anaconda,我的版本是ana ...

  8. Altium Designer (AD) 中规则的部分讲解

    当创建好PCB时,选择 Design - Rules 即可进行规则的设置,也可以直接利用快捷键D-R(多利用快捷键,可以有效的提高设计效率,) 这个是规则的总界面,熟练以后可以直接从这里进行修改,很便 ...

  9. C语言编程学习不难学,是你没找对方法!

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  10. 20155311高梓云补交的Mypc课下实践

    20155311高梓云补交的Mypc课下实践 老师,由于我自己的疏忽导致没有及时交上这次作业.这是我的代码和截图. ``` ```/**import java.io.; import java.lan ...