POJ3784 Running Median
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 1670 | Accepted: 823 |
Description
Input
Output
Sample Input
3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56
Sample Output
1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3
Source
对于每个奇数次读入,输出当前已有序列的中位数
维护两个堆,一个大根堆,一个小根堆。大根堆里存较小的数,小根堆里存较大的数。维护好以后,小根堆顶就是中位数。
每次新加入一个数,若该数比中位数大,存入小根堆,否则存入大根堆。
限制小根堆里的数最多比大根堆大1,若不满足,就把小根堆的堆顶弹到大根堆。
以下是代码,基本是抄的233。
结构体里写的是小根堆,为了缩减代码长度,把大根堆取负,也按照小根堆的算法算,就能维护出大根堆。
注意输出格式。mod20==19是为了输出10个中位数就换一行。
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
struct pile{
int h[];
int cnt;
void insert(int x){//插入
h[++cnt]=x;
int k=cnt;
while(k> && h[k]<h[k>>]){//维护
swap(h[k],h[k>>]);
k>>=;
}
}
void pop(){
int k=;
h[]=h[cnt];//把末尾元素顶上来
h[cnt--]=;
while(k<=cnt){
if(h[k]>h[k+] && k<cnt)k++;//在两个子节点中找较小的
if(h[k]<h[k>>])swap(h[k],h[k>>]),k<<=;//把较小的顶上去
else break;//满足性质,退出
}
}
}big,small,empty;
void ins(int x){
if(x<=-big.h[])
big.insert(-x);
else small.insert(x);
while(small.cnt>big.cnt) big.insert(-small.h[]),small.pop();
while(big.cnt>small.cnt+) small.insert(-big.h[]),big.pop();
}
int n,m;
int main(){
int T;
scanf("%d",&T);
int i,j;
while(T--){
big=small=empty;//初始化
scanf("%d%d",&n,&m);
int num;
printf("%d %d\n",n,(m+)>>);
for(i=;i<=m;i++){
scanf("%d",&num);
ins(num);
if(i&){//奇数
printf("%d",-big.h[]);
if(i==m)printf("\n");else printf(" ");
if(i%==)printf("\n");
}
}
}
return ;
}
POJ3784 Running Median的更多相关文章
- poj3784 Running Median[对顶堆]
由于我不会讲对顶堆,所以这里直接传上一个巨佬的学习笔记. 对顶堆其实还是很容易理解的,想这题的时候自己猜做法也能把没学过的对顶堆给想出来.后来了解,对顶堆主要还是动态的在线维护集合$K$大值.当然也可 ...
- 【POJ3784】Running Median
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
- 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 ...
- 【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
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=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(动态维护中位数)
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
随机推荐
- C语言 常用的时间函数
//时间函数的使用 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include& ...
- UTF-8 带签名和不带签名的区别
就和字面上一样,带签名的UTF-8文件比不带签名的,在文件开头的地方就多了几个16进制字符--[EF BB BF ],这9个字符就是"签名",这样做的好处是让文本处理工具或者浏览器 ...
- BibTex参考文献制作
&1 制作ref.bib文件 在必应学术或者谷歌学术中搜索文章,这里以我硕士的老板和师兄的一篇文章为例: 两种搜索引擎几乎都差不多,一丢丢区别在下面会讲,点击上图中被引数的旁边那个“引用”按钮 ...
- Qt——树的搜索
一.Qt中的树 Qt中树的实现有两种方式.第一种是使用Qt提供的QTreeWidget,很多函数都封装好,比较方便:另一种是通过QTreeView实现,设置它的数据模型,比如使用QStandardIt ...
- OpenGL、Open Inventor、WebGL、Three.js、ARToolkit、JSARToolkit
[准备看的] http://www.hewebgl.com/ http://www.linuxdiyf.com/viewarticle.php?id=399205 http://blog.sina.c ...
- Eclipse系列: Eclipse设置Tomcat启动超时时间
在eclipse的workspace目录下,找到如下文件: .metadata\.plugins\org.eclipse.wst.server.core\servers.xml 如下图所示,然后将它修 ...
- myeclipse 2014新建maven web 项目步骤
首先在myeclipse中配置maven的安装路径: 在pom.xml中加上: 1 2 3 4 5 6 7 8 9 10 <plugins> <plugin&g ...
- swift第一季基础语法
同: 一.基础 同: 1常量和变量 2数据类型和数据类型转换 3别名 不同: 1可选类型optional 2BOOL类型 3元组类型 4断言Assertion 二.基本操作符 同: 1赋值和算术运算及 ...
- 使用线程池模拟处理耗时任务,通过websocket提高用户体验
前言 在文章开始之前,询问一下大家平时工作中后端处理批量任务(耗时任务)的时候,前端是如何告知用户任务的执行情况的? 楼主对这个问题想了下,决定使用websokect将这一过程展现给用户. 于是就有了 ...
- HTML5 中canvas支持触摸屏的签名面板
1.前言 最近实在是太忙了,从国庆之后的辞职,在慢慢的找工作,到今天在现在的这家公司上班大半个月了,太多的心酸泪无以言表,面试过程中,见到的坑货公司是一家又一家,好几家公司自己都只是上一天班就走了,其 ...