POJ3784:Running Median
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html
题目传送门:http://poj.org/problem?id=3784
用一个“对顶堆”动态维护中位数。
一个大根堆维护前半部分的权值,一个小根堆维护后半部分的权值。
新进来一个数如果小于大根对的权值就加进大根对,否则就加进小根堆。
每次动态维护大小,使得大根堆的大小为数字的一半。
大根对的堆顶就是中位数。
为了方便我把大根堆里的数取了个反也就变小根堆了。
时间复杂度:\(O(Tnlogn)\)
空间复杂度:\(O(n)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e4;
int ans[maxn];
int data,n,cnt;
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct Heap {
int tot;
int tree[maxn];
void ins(int v) {
tree[++tot]=v;
int pos=tot;
while(pos>1) {
if(tree[pos]<tree[pos>>1])
swap(tree[pos],tree[pos>>1]),pos>>=1;
else break;
}
}
int pop() {
int res=tree[1];
tree[1]=tree[tot--];
int pos=1,son=2;
while(son<=tot) {
if(son<tot&&tree[son|1]<tree[son])son|=1;
if(tree[son]<tree[pos])
swap(tree[son],tree[pos]),pos=son,son=pos<<1;
else break;
}
return res;
}
}T1,T2;
int main() {
int T=read();
while(T--) {
data=read(),n=read();
T1.tot=T2.tot=cnt=0;
for(int i=1;i<=n;i++) {
int x=read();
if(x<=(-T1.tree[1]))T1.ins(-x);
else T2.ins(x);
while(T2.tot<i/2) {
int res=T1.pop();
T2.ins(-res);
}
while(T2.tot>i/2) {
int res=T2.pop();
T1.ins(-res);
}
if(i&1)ans[++cnt]=-T1.tree[1];
}
printf("%d %d\n",data,cnt);
for(int i=1;i<=cnt;i++)
if(i%10)printf("%d ",ans[i]);
else printf("%d\n",ans[i]);
if(cnt%10)puts("");
}
return 0;
}
POJ3784:Running Median的更多相关文章
- 【POJ3784】Running Median
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
- POJ 3784.Running Median
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
- AVD启动报错:Running an x86 based Android Virtual Device (AVD) is 10x faster
1.cmd窗口中输入emulator -avd test 启动AVD时报错: Running an x86 based Android Virtual Device (AVD) is 10x fast ...
- JUC线程池之 线程池的5种状态:Running, SHUTDOWN, STOP, TIDYING, TERMINATED
线程池有5种状态:Running, SHUTDOWN, STOP, TIDYING, TERMINATED. 线程池状态定义代码如下: private final AtomicInteger ctl ...
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- POJ3784 Running Median
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1670 Accepted: 823 Description For th ...
- poj3784 Running Median[对顶堆]
由于我不会讲对顶堆,所以这里直接传上一个巨佬的学习笔记. 对顶堆其实还是很容易理解的,想这题的时候自己猜做法也能把没学过的对顶堆给想出来.后来了解,对顶堆主要还是动态的在线维护集合$K$大值.当然也可 ...
随机推荐
- 详解Maven项目利用java service wrapper将Java程序生成Windows服务
在项目的开发中,有时候需要将Java应用程序打包成Windows服务,我们就直接可以通过windows的服务来启动和关闭java程序了. 本博文将通过有两种方法实现该功能,手动创建法和Maven自动打 ...
- 深入 JavaScript 中的对象以及继承原理
ES6引入了一个很甜的语法糖就是 class, class 可以帮助开发者回归到 Java 时代的面向对象编程而不是 ES5 中被诟病的面向原型编程. 我也在工作的业务代码中大量的使用 class, ...
- R语言做正态性检验
摘自:吴喜之:<非参数统计>(第二版),中国统计出版社,2006年10月:P164-165 1.ks.test() 例如零假设为N(15,0.2),则ks.test(x," ...
- HDU - 1800 Flying to the Mars 【贪心】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1800 题意 给出N个人的 level 然后 高的level 的 人 是可以携带 比他低level 的人 ...
- NSAttributedStringKey
NSFontAttributeName; //字体,value是UIFont对象 NSParagraphStyleAttributeName;//绘图的风格(居中,换行模式,间距等诸多风格),valu ...
- 每天一个Linux命令(23)chmod命令
chmod命令用来变更文件或目录的权限. 在UNIX系统家族里,文件或目录权限的控制分别以读取.写入.执行3种一般权限来区分,另有3种特殊权限可供运用.用户可以使用chmod指令去变更文件与目 ...
- java常用注解(更新中)
注解根据来源可分为: 系统注解(自带的,取决于JDK版本).自定义注解及第三方注解 系统注解根据用途又可分为: java内置注解和元注解 根据运行机制(保留到什么时候)可分为: 源码注解.编译注解和运 ...
- 第四篇、javascript
一.正则表达式 提示:此专题需要多轮复习反复的加深和理解 正则表达式的两种用法: 1)regexp.xxx(string); 2)string.yyy(regexp); 验证用户输入的手机号格式是否合 ...
- github与eclipse结合使用
github是现在流行的代码托管平台,今天以eclipse为例讲解github的使用,新建项目,提交eclipse项目到github,新建分支,合并分支 1.github上新建项目 记下githug项 ...
- SpringMVC的优点
SpringMVC的优点 清晰的角色划分:控制器(controller).验证器(validator).命令对象(command obect).表单对象(form object).模型对象(model ...