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. eclipse 打开一个新工程的基本设置

    1.代码自动提示 Window -> Preferences -> Java -> Editor -> Content Assist -> Auto Activation ...

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

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

  3. Mongo DB命令简介

    引言   最近在学习MongoDB 总结了一些命令及常用的东西做整理   常用目录文件介绍 mongod 数据库部署命令 mongo 连接mongodb数据库而使用的命令 mongoimport 导入 ...

  4. php 面试常问 基础知识

    字符串都是海针 数组是针海 输出及打印 echo 8%(-2); //输出结果为0 模后的正负号可无视 模前的有用 echo date('Y-m-d H:i:s',strtotime('-1 day' ...

  5. 07.centos7构建 IntelliJ IDEA(简称IDEA)开发环境

    一.安装IDEA 进入官网下载linux版的社区便,IDEA分为社区版和旗舰版,社区版免费,并且基本满足spark开发需求. 解压安装 目录为/opt/idea 注意:centos命令行界面下是无法安 ...

  6. Java ArrayList 源代码分析

    Java ArrayList 之前曾经参考 数据结构与算法这本书写过ArrayList的demo,本来以为实现起来都差不多,今天抽空看了下jdk中的ArrayList的实现,差距还是很大啊 首先看一下 ...

  7. 基于visual studio 2017 以及cubemx 搭建stm32的开发环境(1)

    参考如下文档: 传送门:http://www.stm32cube.com/article/128 如果链接不存在的话,下载我截屏好的图: 传送门:https://pan.baidu.com/s/1NC ...

  8. [Java算法分析与设计]--链式堆栈的设计

    在上篇文章当中,我们实现了底层为数组的顺序栈.在我之前的文章中也提到过:以数组为数据结构基础在存储数据方面需要一整块连续的内存来存放数据,一旦遇到需要可以动态扩展的功能需求时如果数据量大可能会给虚拟机 ...

  9. Oracle入门第二天(下)——单行函数

    一.概述 以下内容完整参阅,参考官方文档函数手册部分:https://docs.oracle.com/cd/E11882_01/nav/portal_5.htm 离线chm手册英文版:链接:https ...

  10. 第六周课上测试-3-ch02补充作业

    实验要求: 编写一个程序 "week0603学号.c",运行下面代码: short int v = -学号后四位 unsigned short uv = (unsigned sho ...