Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1670   Accepted: 823

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

Source

对于每个奇数次读入,输出当前已有序列的中位数

维护两个堆,一个大根堆,一个小根堆。大根堆里存较小的数,小根堆里存较大的数。维护好以后,小根堆顶就是中位数。

每次新加入一个数,若该数比中位数大,存入小根堆,否则存入大根堆。

限制小根堆里的数最多比大根堆大1,若不满足,就把小根堆的堆顶弹到大根堆。

以下是代码,基本是抄的233。

结构体里写的是小根堆,为了缩减代码长度,把大根堆取负,也按照小根堆的算法算,就能维护出大根堆。

注意输出格式。mod20==19是为了输出10个中位数就换一行。

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
struct pile{
int h[];
int cnt;
void insert(int x){//插入
h[++cnt]=x;
int k=cnt;
while(k> && h[k]<h[k>>]){//维护
swap(h[k],h[k>>]);
k>>=;
}
}
void pop(){
int k=;
h[]=h[cnt];//把末尾元素顶上来
h[cnt--]=;
while(k<=cnt){
if(h[k]>h[k+] && k<cnt)k++;//在两个子节点中找较小的
if(h[k]<h[k>>])swap(h[k],h[k>>]),k<<=;//把较小的顶上去
else break;//满足性质,退出
}
}
}big,small,empty;
void ins(int x){
if(x<=-big.h[])
big.insert(-x);
else small.insert(x);
while(small.cnt>big.cnt) big.insert(-small.h[]),small.pop();
while(big.cnt>small.cnt+) small.insert(-big.h[]),big.pop();
}
int n,m;
int main(){
int T;
scanf("%d",&T);
int i,j;
while(T--){
big=small=empty;//初始化
scanf("%d%d",&n,&m);
int num;
printf("%d %d\n",n,(m+)>>);
for(i=;i<=m;i++){
scanf("%d",&num);
ins(num);
if(i&){//奇数
printf("%d",-big.h[]);
if(i==m)printf("\n");else printf(" ");
if(i%==)printf("\n");
}
}
}
return ;
}

POJ3784 Running Median的更多相关文章

  1. poj3784 Running Median[对顶堆]

    由于我不会讲对顶堆,所以这里直接传上一个巨佬的学习笔记. 对顶堆其实还是很容易理解的,想这题的时候自己猜做法也能把没学过的对顶堆给想出来.后来了解,对顶堆主要还是动态的在线维护集合$K$大值.当然也可 ...

  2. 【POJ3784】Running Median

    Running Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3406   Accepted: 1576 De ...

  3. hdu 3282 Running Median

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...

  4. POJ 3784.Running Median

    2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...

  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 (对顶堆)

    Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...

  7. POJ3784:Running Median

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=3784 用一个"对顶堆& ...

  8. Running Median POJ - 3784 (对顶堆/优先队列 | 链表)

    For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...

  9. POJ 3784 Running Median(动态维护中位数)

    Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...

随机推荐

  1. Linux下php安装memcache扩展

    安装环境:CentOS 6.4 php扩展memcache的作用是为了支持memcached数据库缓存服务器,下面是安装方法. 1.下载 下载地址:http://pecl.php.net/packag ...

  2. SVN版本回退

    [SVN版本回退] 在Windows里,先打开Log面板,根据想要回退的内容,然后选择revert to this revision或者revert changes from this revisio ...

  3. 转:程序员必须知道的几个Git代码托管平台

    http://www.open-open.com/lib/view/open1420704561390.html

  4. js定时器的一些小问题

    1 js中定时器分为两种:setInterval和setTimeout, 但是在代码的执行中,定时器的优先级最低,系统里其他不在执行的时候,它才开始.例子如下: 2 3 <script> ...

  5. for循环的三种写法

    第一种写法  传统的方法,遍历数组 String[] arr = { "amy", "heinrich", "cindy", "g ...

  6. 七、Block 封装代码

    1.概念:封装代码块,调用的时候使用 2.声明 返回类型(^名字)(参数1,参数2..) = (参数类型 变量1,参数类型, 变量2){ }; int (^Sum)(int,int)  = ^(int ...

  7. 图解HTTP看书体会(1)

    MAC地址和IP地址的区别与联系 一.IP地址 对于IP地址,相信大家都很熟悉,即指使用TCP/IP协议指定给主机的32位地址.IP地址由用点分隔开的4个8八位组构成,如192.168.0.1就是一个 ...

  8. 【转】【WPF】TemplateBinding和Binding的区别

    定义 TemplateBinding是为了某个特定场景优化出来的数据绑定版本--需要把ControlTemplate里面的某个Property绑定到应用该ControlTemplate的控件的对应Pr ...

  9. 【转】【Asp.Net】Asp.net发送邮件的两种方法小结

    这几天看了一下Asp.net发送邮件方面的东西,记得之前的IIS6上有SMTP服务器,可以直接利用这个进行邮件发送,现在的开发环境是Windows 7,找了半天没有找到,到网络上查了才知道原来wind ...

  10. [转]hadoop hdfs常用命令

    FROM : http://www.2cto.com/database/201303/198460.html hadoop hdfs常用命令   hadoop常用命令:  hadoop fs  查看H ...