本题使用对顶堆做法。

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

用法:在动态维护的过程中,设当前的长度为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 项目抛出警告

    There are multiple modules with names that only differ in casing. 此图为 博主(初雪日)的截图, 这个问题虽然不报错,但是会对项目有影 ...

  2. 解决Linux-Centos7启动Mysql服务失败丢失mysql.sock问题

    在新安装mysql后进行启动发现报错 mysql启动服务命令 systemctl start mysqld@3306 Starting mysqld (via systemctl):  Job for ...

  3. 【问题记录】—.NetCore 编译问题

    最近在协助验证Jenkins自动编译发布时,对一些.Net Core编译问题进行了解决:特记录一下 一.编译生成netcoreapp目录问题 问题现象 .net core项目编译输出目录总是包含在[n ...

  4. K8S 使用 SideCar 模式部署 Filebeat 收集容器日志

    对于 K8S 内的容器日志收集,业内一般有两种常用的方式: 使用 DaemonSet 在每台 Node 上部署一个日志收集容器,用于收集当前 Node 上所有容器挂载到宿主机目录下的日志 使用 Sid ...

  5. 2020 10月CUMTCTF wp

    华为杯 × 签到杯√ 论比赛过程来说没什么很大收获 但看师傅们的wp感触很多 赛后复现慢慢学吧 Web babyflask flask ssti模板注入: payload{{key}}发现[]以及类似 ...

  6. 如何使用Internet Explorer下载安装最新版Edge浏览器

    这个题目看起来可能有点奇怪,不过最近这段时间, 在一个刚安装完的Windows计算机上,确实是一个需要解决的问题.2020年8月中旬,微软宣布:一年之后,Microsoft 365 应用与服务将不再支 ...

  7. SpringBoot原理发现(一)

    说明: 本系列基于SpringBoot 2.2.9.RELEASE 版本,对SpringBoot的原理进行分析,一共分为四节: SpringBoot原理发现(一):创建Hello World,对pom ...

  8. 用GitHub Pages搭建博客(三)

    本篇介绍通过git工具替换网站主题,并发布 Jekyll和Hexo的简要介绍   GitHub Pages是基于Jekyll构建的,Jekyll 是一个简单的博客形态的静态站点生产工具,它有一个模版目 ...

  9. 第4章 Function语意学

    第4章 Function语意学 目录 第4章 Function语意学 4.1 Member的各种调用方式 Nonstatic Member Function(非静态成员函数) virtual Memb ...

  10. 阿里云函数计算 VSCode 使用,及部署 Docusaurus

    代码: https://github.com/ikuokuo/start-serverless 使用简介 产品页开通服务.使用流程,如下: 新手示例,如下: 创建函数 阿里云提供了如下几种方式创建函数 ...