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. TestLink学习三:发送邮件的两种配置方法

    第一种:修改config.inc.php中的[smtp],配置为默认本地发送,用hotmail用户做接收,调试成功!(本人未尝试这种) // ----------------------------- ...

  2. (转) C#多线程赛跑实例

    专于:http://blog.csdn.net/lidatgb/article/details/8363035 结合上篇<多线程的基础>,这次我们写一个多线程的赛跑实例,内容很简单:超人和 ...

  3. xcode的菜单栏功能解析

    [Xcode 7.2]Xcode菜单栏之你不知道的那点事 File: New : 可以新建tap,窗口,新文件,playground,workspace,target等等. Add Files to ...

  4. 024医疗项目-模块二:药品目录的导入导出-HSSF导入类的学习

    我们之前学习了怎么把数据的数据导出来保存到Excle中,这篇文章我们学习怎么Excel数据导出然后插入到数据库中. 我们先学习HSSF怎么用来导出数据. 看官方教程步骤如下: 第一步: 创建一个wor ...

  5. Uploadify v3.2.1 属性、事件、方法说明

    一.属性 属性名称 默认值 说明 auto true 设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 . buttonClass " 按钮样式 buttonCur ...

  6. ASP.Net MVC如何访问的静态页面

    MVC开发中,因为View文件夹下的web.config文件默认会把任何方法的请求的任何文件,路径都交给 System.Web.HttpNotFoundHandler 去处理.起到Controller ...

  7. 【高清未加密】2015传智播客 最新21期c#asp.net 基础到就业班视频和源码

    [.NET]传智播客第[21]期就业班视频(高清无加密)本套2015年21期传智播客C#ASP.NET win10通用mvc+app开发视频教程附源码,是一套非常不错的asp.net自学视频教程,传智 ...

  8. [MetaHook] Quake FMOD function

    QFMOD.h #ifndef QFMOD_H #define QFMOD_H #include "fmod.h" extern FMOD_RESULT (F_API *qFMOD ...

  9. 《Linux内核设计与实现》课程学习重点问题总结

    (问题均是同学提出或是老师上课重点讲解的部分内容,根据自身理解和笔记总结出自己的答案.如有不对,还请指教.) week2 [Q1]命令qemu -kernel 内核可执行文件 -initrd root ...

  10. 从Swift学习iOS开发的路线指引

    本文主要是楼主近段时间从Swift语法开始自学iOS开发的流程总结,PS 一个前提,楼主的生存环境中买不到一本iOS开发中文教程,所以基本都是百度摸索出来的 >_< 主要流程 学习Swif ...