Running Median

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

 
 
【题意】
  一组数按顺序加入数组,每奇数次加入的时候就输出中位数
 
【分析】
  持续维护区间第k小可以用对顶堆,一个是存小数据的大根堆,一个是存大数据的小根堆。
   
 
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 10010 priority_queue<int > q1;
priority_queue<int,vector<int>,greater<int> > q2; int c1,c2,num,n;
int a[Maxn]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
c1=c2=;
while(!q1.empty()) q1.pop();
while(!q2.empty()) q2.pop();
num=;
scanf("%d%d",&num,&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
printf("%d %d\n",num,(n+)/);
for(int i=;i<=n;i++)
{
if(q2.empty()||a[i]<=q2.top()) q1.push(a[i]);
else q2.push(a[i]);
while(q2.size()>=(i+)/) {q1.push(q2.top());q2.pop();}
while(q1.size()>(i+)/) {q2.push(q1.top());q1.pop();}
if(i%==&&i!=n) printf("%d ",q1.top());
else if(i%==) printf("%d",q1.top());
if(i%==) printf("\n");
}printf("\n");
}
return ;
}

2017-01-18 09:18:12

【POJ 3784】 Running Median (对顶堆)的更多相关文章

  1. POJ 3784.Running Median

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

  2. poj3784 Running Median[对顶堆]

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

  3. POJ 3784 Running Median【维护动态中位数】

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

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

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

  5. POJ 3784 Running Median (动态中位数)

    题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...

  6. POJ 3784 Running Median (模拟水过带翻译)

    Description Moscow is hosting a major international conference, which is attended by n scientists fr ...

  7. POJ3784:Running Median

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

  8. 【POJ 3784】 Running Median

    [题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...

  9. Running Median POJ - 3784

    本题使用对顶堆做法. 为了动态维护中位数,我们可以建立两个堆 :一个大根对,一个小根堆. 用法:在动态维护的过程中,设当前的长度为length,大根堆存从小到大排名 $1 \thicksim \dfr ...

随机推荐

  1. Item27--优先考虑泛型方法

    类型推导:发生在以下三个地方.1.Java编译器根据泛型方法传入的参数,推导出具体的类型.2.Java编译器,根据泛型构造器传入的类型来推导出实际要构造的实例类型.3.Java编译器根据表达式的目标类 ...

  2. 【CodeForces】896 B. Ithea Plays With Chtholly

    [题目]B. Ithea Plays With Chtholly [题意]交互题,有n格,每次给一个[1,c]的数字,回答填入的位置后再次给数字,要求在m轮内使n格填满且数列不递减.n,m>=2 ...

  3. 【转载】VS2013安装需要IE10

    因为需要移动办公,需要给笔记本搭建编程环境.安装VS2013时遇到了小麻烦,提示我,需要安装IE10. 然后我很听话的按照提供的超链接,到了官网,下载了最新的IE11,然后安装,结果告诉我下载的IE版 ...

  4. jq_从右向右的滑入滑出效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. Python代码这样写更优雅(转)

    1.变量交换 大部分编程语言中交换两个变量的值时,不得不引入一个临时变量: >>> a = 1>>> b = 2>>> tmp = a>&g ...

  6. jstorm相关

    https://www.cnblogs.com/antispam/p/4182210.html

  7. 【bzoj1068】【SCOI2007】压缩

    一道区间dp f[i][j][0/1]表示[i,j]区间是否加入M,并且之前一位有M的最小长度 可以理解为在第一位之前有一个M 那么就可以转移了. #include<bits/stdc++.h& ...

  8. HDU 5136 Yue Fei's Battle

    题目链接:HDU-5136 网上的一篇题解非常好,所以就直接转载了.转自oilover的博客 代码: #include<cstring> #include<cstdio> #i ...

  9. nodejs面试题

    1.为什么用Nodejs,它有哪些缺点? 事件驱动,通过闭包很容易实现客户端的生命活期. 不用担心多线程,锁,并行计算的问题 V8引擎速度非常快 对于游戏来说,写一遍游戏逻辑代码,前端后端通用 当然N ...

  10. MYSQL表中向SET类型的字段插入值时值之间不能有空格

    MYSQL 中有一种数据类型是 SET,首先我们查看一个包含 SET 类型字段的表结构: 接下来我们向表中插入数据: 按照上面的语句插入数据发现报错了,于是去掉了插入值之间的空格,然后插入成功: