本题使用对顶堆做法。

为了动态维护中位数,我们可以建立两个堆 :一个大根对,一个小根堆。

用法:在动态维护的过程中,设当前的长度为length,大根堆存从小到大排名 $1 \thicksim \dfrac{m}{2} $ 的整数,小根堆存小到大排名 $ \dfrac{m}{2} + 1 \thicksim m $ 的整数

如何动态维护?顾名思义,动态,即边输入边处理。显然,为了维护中位数,我们还要不断地维护两个堆的\(size\)

每次新读入一个值,就 \(\begin{cases}插入大根堆&x < 中位数\\插入小根堆&x\geqslant中位数\end{cases}\) ,然后维护。

\({\color{red}{注意这一题的输出!}}\)

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
priority_queue<int,vector<int>,greater<int> >xg;//С¸ù¶Ñ
priority_queue<int,vector<int>,less<int> >dg;//´ó¸ù¶Ñ
int T;
void maintenance(){
int l1 = dg . size();
int l2 = xg . size();
while(l1 > l2){
int tmp = dg . top();
dg . pop();
xg . push(tmp);
l1 --;
l2 ++;
// l1 = dg . size();
// l2 = xg . size();
}
while(l2 - l1 > 1){
int tmp = xg . top();
xg . pop();
dg . push(tmp);
// l1 = dg . size();
// l2 = xg . size();
l1 ++;
l2 --;
}
}
void work(){
while(!dg.empty())dg.pop();
while(!xg.empty())xg.pop();
int id, n, data,cnt = 0;
cin >> id >> n;
cout << id << " " << (n + 1) / 2 << endl;
for(int i = 1;i <= n;i ++){
cin >> data;
if(i == 1){
xg . push(data);
} else if(data < xg . top()){
dg . push(data);
// cout << "indg";
maintenance();
} else {
xg . push(data);
maintenance();
}
if(i & 1){
cout << xg . top() ;
cnt ++ ;
if(i == n || cnt % 10 == 0)
cout << endl;
else cout << " ";
}
}
}
int main(){
// freopen("RMPOJ.in","r",stdin);
// freopen("RMPOJ.out","w",stdout);
ios :: sync_with_stdio(false);
cin >> T;
while(T --){
work();
}
return 0;
}

Running Median POJ - 3784的更多相关文章

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

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

  2. POJ 3784.Running Median

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

  3. 【POJ 3784】 Running Median (对顶堆)

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

  4. hdu 3282 Running Median

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

  5. HDU 3282 Running Median 动态中位数,可惜数据范围太小

    Running Median Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  6. 【POJ3784】Running Median

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

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

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

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

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

  9. 【POJ 3784】 Running Median

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

随机推荐

  1. vue生命钩子函数

    vue的生命钩子函数在使用Vue开发中是非常重要的一环,可以说,生命钩子函数使开发变得更加便捷. 下图是Vue的生命周期图: 具体钩子如下: beforeCreate created beforeMo ...

  2. 手把手教你使用 Prometheus 监控 MySQL 与 MariaDB.md

    概述 MySQL 是常用的关系型数据库,MariaDB 作为 MySQL 的分支版本,兼容 MySQL 协议,也越来越流行.在 Kubernetes 环境中如何使用 Prometheus 来对它们进行 ...

  3. PageHelper使用步骤

    一.导入jar包(maven构建导入坐标) <dependency> <groupId>com.github.pagehelper</groupId> <ar ...

  4. [Luogu P3469] [POI2008]BLO-Blockade (割点)

    题面 传送门:https://www.luogu.org/problemnew/show/P3469 Solution 先跟我大声念: poi! 然后开始干正事. 首先,我们先把题目中的点分为两类:去 ...

  5. CF1413C Perform Easily 题解

    毒瘤C题,考场卡我1个小时 首先,这道题难点在哪里?它的最大值与最小值都是浮动的. 怎么办?把最小/最大值固定! 以把最小值固定为例,我们枚举每个音符,并枚举它使用哪条琴弦,将它此时的位置强制其作为最 ...

  6. 遗传算法(Genetic Algorithm)——基于Java实现

    一.遗传算法原理介绍 遗传算法(Genetic Algorithm)是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法.遗传算法是从代表问 ...

  7. leetcode103:permutations-ii

    题目描述 给出一组可能包含重复项的数字,返回该组数字的所有排列 例如: [1,1,2]的排列如下: [1,1,2],[1,2,1], [2,1,1]. Given a collection of nu ...

  8. icmp port unreachable

    端口不可达: client------>server 结果server回复端口不可达, 由于是icmp报文: 到达client内核协议栈后进入icmp_rcv处理: /* * Deal with ...

  9. tcpack--4延时ack

    TCP在收到数据后必须发送ACK给对端,但如果每收到一个包就给一个ACK的话会使得网络中被注入过多报文.TCP的做法是在收到数据时不立即发送ACK,而是设置一个定时器,如果在定时器超时之前有数据发送给 ...

  10. ceph的pg的分布的快速查看

    前言 本篇的内容实际上是另外一篇文章的字篇章,在另外一篇文章当中,将会对crush的分布的调整的做一次总结,用比较简单的方式来展示各种crush的区别 在做这个工作过程中,为了更好的能展示出效果,就有 ...