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小的数放在小根堆 ...
随机推荐
- window 属性:自定义元素(custom elements)
概述 Web Components 标准非常重要的一个特性是,它使开发者能够将HTML页面的功能封装为 custom elements(自定义标签),而往常,开发者不得不写一大堆冗长.深层嵌套的标 ...
- Linux文件操作常用命令
一.一些文件操作命令. 1.cd /home 进入"home目录" 2.cd ../ 返回上一级目录 3.cd - 返回上次所在的目录 4.pwd 显示工程路径 5.ll 显示 ...
- abp(net core)+easyui+efcore实现仓储管理系统——出库管理之四(五十三)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统--ABP总体介绍(一) abp(net core)+ ...
- Verification of WISHBONE I2C Master Core(IRUN+Simvision)
一.前言 很久没写技术博客了,有些懈怠,生活还得继续折腾.转眼工作一年多,时间越长越发觉得自己知之甚少,当然这跟IC行业技术密集有关.用空余时间在opencores网站上下载些小的IP看看 验证下,让 ...
- 新版本 swagger 组件中 Servers 的 坑
新版本 Swashbuckle swagger 组件中 Servers 的 坑 Intro 上周做了公司的项目升级,从 2.2 更新到 3.1, swagger 直接更新到了最新,swagger 用的 ...
- Spring MVC 9大组件概述
SpringMVC中的Servlet一共有三个层次,分别是HttpServletBean.FrameworkServlet和 DispatcherServlet.HttpServletBean直接继承 ...
- 常用-DNS
1.1.1.1 cloudflare 1.0.0.1 119.29.29.29 腾讯 114.114.114.114 114.114.115.115 114.114.114.119财务 114.114 ...
- C++ 基础 6:模板
1 函数模板 泛型(Generic Programming),即是指具有在多种数据类型上皆可操作的含意. 泛型编程 的代表作品 STL 是一种高效.泛型.可交互操作的软件组件. 泛型编程最初诞生于 C ...
- sqlserver with(NOLOCK) 或 with(READPAST)
https://blog.csdn.net/shuicaohui5/article/details/6758868
- 10before_request钩子函数
1,什么是钩子函数? 就是运行别人前都得先运行他: from flask import Flask app = Flask(__name__) @app.route('/') def hello_wo ...