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$大值.当然也可 ...
随机推荐
- api签名认证
参数列表: data: { sign, uid或是openId, version, timestamp, param } sign 签名一般情况下,根据如下几项生成,通过md5或是aes加密: 接口 ...
- Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix
http://mathworld.wolfram.com/Moore-PenroseMatrixInverse.html 显然,埃尔米特矩阵主对角线上的元素都是实数的,其特征值也是实数.对于只包含实数 ...
- $ php app/console fos:user:promote
$ php app/console fos:user:create Please choose a username:admin Please choose an email:admin21@dwq ...
- SUBMIT 用法
[转自http://lz357502668.blog.163.com/blog/static/16496743201241195817597/] 1.最普通的用法 *Code used to exec ...
- RLearning第2弹:创建数据集
任何一门语言,数据类型和数据结构是最基础,也是最重要的,必须要学好!1.产生向量 a<-c(1,2,5,3,6,-2,4) b<-c("one","two&q ...
- JETSON TK1 ~ 基于eclipse下开发ROS
此文档是在PC端开发后移植到TK1,并非在TK1上安装eclipse 官方使用IDE开发的文档: http://wiki.ros.org/IDEs 一:安装eclipse 1.下载eclipse安装包 ...
- MVC ViewBag不能使用在工程文件中添加引用
在工程文件中 <ItemGroup> // ... </ItemGroup> 添加引用 <Reference Include="Microsoft.CSharp ...
- [原创]关于tomcat启动时时候端口被占用,8080,8005,8009
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- mysql 触发器(trigger) 总结
触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...
- Spring与web.xml
出处:http://blog.csdn.net/u010796790 在web.xml配置监听器 ContextLoaderListener (listener-class) ContextLoade ...