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$大值.当然也可 ...
随机推荐
- php读写csv、xml文件: SimpleExcel
实例结构: 1. csv2xml.demo.php <?php use SimpleExcel\SimpleExcel; // 这句不能少! require_once ('../lib/Simp ...
- 【python】-- web开发之DOM
DOM 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是, ...
- windows下安装PyQt4
第一步:确认自己电脑上的Python版本.然后下载对应的.whl文件下载 第二步:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4上下载对应版本版本的 ...
- 将本地jar包手动复制到Maven库中,在其它电脑上用Maven打包时出错
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/UP19910522/article/details/31396107 背景交代:在做图片水印时候引入 ...
- vMware存储:SAN配置基础
VMware存储不仅仅是将LUN映射给物理服务器这么简单.VMware vSphere允许系统管理员在一个物理机上创建多个虚拟机. 潜在的hypervisor和vSphere ESXi,能够使gues ...
- value too great for base (error token is "08")
shell 中,经常有定时任务, 这时候shell脚本中一般会对时间进行一些判断,或者相关逻辑的操作 这时候,如果你获取的小时或者分钟是08,09,如果要再对其进行运算符或者比较的话,就会报标题的错误 ...
- ThinkPHP 3.2.2 事务
手册里说得非常清楚 : 5.3.19 事务支持 ThinkPHP提供了单数据库的事务支持,如果要在应用逻辑中使用事务,可以参考下面的方法: 启动事务: PHP代码 $User->startTra ...
- 牛客小白月赛1 B 简单题2 【数学】
题目链接 https://www.nowcoder.com/acm/contest/85/B 思路 这个 也是 E AC代码 #include <cstdio> #include < ...
- 事件监听机制——列出指定目录内容、添加Dialog对话框
事件监听机制理解与Dialog练习 利用Java语言,仿照我的电脑目录进行打开目录,输入文件路径,查看该路径下所有的文件,设置两个文本框,一个转到按钮,当点击转到按钮时,查看路径是否正确,若正确在第二 ...
- Data Structure Linked List: Detect and Remove Loop in a Linked List
http://www.geeksforgeeks.org/detect-and-remove-loop-in-a-linked-list/ #include <iostream> #inc ...