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. JavaScript小练习1-控制div属性

    题目 要实现的效果如图所示:查看演示 *** 分析 乍一看还以为十分简单,就是简单的点击button时触发的函数来改变样式值,不过做到后面就开始打脸了--"重置"功能.其实要实现重 ...

  2. c++友元函数、友元类、友成员函数

    友元函数:不是类成员函数,是一个类外的函数,但是可以访问类所有成员. class Point{ public: friend void fun(Point t);//友元函数 private: int ...

  3. C++笔记015:C++对C的扩展——三目运算符功能增强

    原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 三目运算符在C编译器中的表现: int main() { int a=10; int b=20; //三目运算符是一个表达式,表达式不能做 ...

  4. 延迟加载图片控件--echo.js

    echo.js的github地址:https://github.com/toddmotto/echo   echo是一个独立的JavaScript.轻量级的.延迟图片加载插件,echo压缩后体积不到1 ...

  5. yii学习笔记(2),创建控制器

    将网站根目录配置到项目的web目录 打开网站访问的是web/index.php这时打开默认页面 访问一下其他页面,发现浏览器地址的url携带了一个参数 http://www.test.com/inde ...

  6. T+API HTTPServer服务端

    该服务端是一个HTTP服务器,这样其他语言调用也方便. 出于某些原因,只支持Post方法,不打算支持其他方法,例如Get. API所接受的参数将以Json传送,回传的数据也是一个Json数据,一切只是 ...

  7. 2.4 自己编写一个vivi驱动程序

    学习目标:从零编写一个vivi驱动程序,并测试: 一. vivi驱动应用程序调用过程 上节对xawtv对vivi程序调用欧城进行了详细分析,可总结为以下流程: 二.仿照vivi.c编写myvivi.c ...

  8. 嵌入式Linux 网络编程

    涉及到的数据结构: 下面首先介绍两个重要的数据类型:sockaddr和sockaddr_in,这两个结构类型都是用来保存socket地址信息的 定义如下所示: struct sockaddr { un ...

  9. SAP BDC 交货增强无法进入

    最近碰到一个问题,使用BDC MODE 'N' UPDATE 'S' 无法进入增强: FV50XF0B_DELIVERY_PUBLISH DELIVERY_PUBLISH_AFTER_SAVE 前台操 ...

  10. 20155313 2016-2017-2 《Java程序设计》第一周学习总结

    20155313 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 本周的Java学习进入了一个全新的阶段,对于我这样的并没有每天花费时间钻研的同学来说,最后几 ...