Running Median POJ - 3784
本题使用对顶堆做法。
为了动态维护中位数,我们可以建立两个堆 :一个大根对,一个小根堆。
用法:在动态维护的过程中,设当前的长度为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的更多相关文章
- Running Median POJ - 3784 (对顶堆/优先队列 | 链表)
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...
- POJ 3784.Running Median
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 【POJ3784】Running Median
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
- POJ 3784 Running Median【维护动态中位数】
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- POJ 3784 Running Median(动态维护中位数)
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- 【POJ 3784】 Running Median
[题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...
随机推荐
- 实在解决不了丢失vs2019之类的msvcr110.dll之类的问题
因为msvcr110.dll也是微软DirectX的一个组件 如果在下载VC运行库没用的情况下,可能是因为要运行的程序是win32的,但是电脑和下载的程序是64的,所以 下载一个win32的即可 如果 ...
- pandas dataframe 时间字段 diff 函数
pandas pandas 是数据处理的利器,非常方便进行表格数据处理,用过的人应该都很清楚,没接触的可以自行查阅pandas 官网. 需求介绍 最近在使用 pandas 的过程中碰到一个问题,需要计 ...
- liunx命令的运用
工作中用到了一些命令,记忆才深刻 1.查看服务器内存:free -h 2.查看服务器磁盘空间:df -h 3.切root用户:sudo su root 输入密码 4.查看liunx服务器下的所有用户: ...
- 【转】Hello SDL: Your First Graphics Window
FROM: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php Hello SDL: Your First Graphics Window ...
- AQS源码深入分析之独占模式-ReentrantLock锁特性详解
本文基于JDK-8u261源码分析 相信大部分人知道AQS是因为ReentrantLock,ReentrantLock的底层是使用AQS来实现的.还有一部分人知道共享锁(Semaphore/Count ...
- 删除指定路径下指定天数之前(以文件的最后修改日期为准)的文件:BAT + VBS
代码如下: @echo off ::演示:删除指定路径下指定天数之前(以文件的最后修改日期为准)的文件. ::如果演示结果无误,把del前面的echo去掉,即可实现真正删除. ::本例调用了临时VBS ...
- 关于windows下activeMQ的安装
1.下载地址http://activemq.apache.org/activemq-5154-release.html 2.修改登录账号和密码,在配置文件jetty-realm.properties中 ...
- linux 查看和设置主机名
1.设置主机名 通过编辑/etc/sysconfig/network文件中的HOSTNAME字段就可以修改主机名.如下所示: [root@zijuan /]# vim /etc/sysconfig/n ...
- How to refresh datasource args caller[X++]
To refresh datasource args caller, you must add override method close on form like source code belo ...
- npm--npm+gulp发布至私服,报错E503解决方案
由于项目共享组件库的需要,我们搭建了npm私有服务器,供本公司几个项目可以访问.组件库使用gulp+webpack+npm进行打包构建,私服使用的是 Verdaccio直接搭建的,一键式傻瓜搭建,贼好 ...