poj3784 Running Median[对顶堆]
由于我不会讲对顶堆,所以这里直接传上一个巨佬的学习笔记。
对顶堆其实还是很容易理解的,想这题的时候自己猜做法也能把没学过的对顶堆给想出来。后来了解,对顶堆主要还是动态的在线维护集合$K$大值。当然也可以带删除。但是可能退化,具体见另外一个题的说明。
像这题维护中位数就是要求第$N/2+1$大的数,所以可以让大根堆维护前$n/2$项,小根堆维护后$n/2+1$项(这里指排好序的)。然后每次插入的时候及时调整即可,由于调整幅度不大,所以可以保证复杂度是$log$级别的。
代码被我写繁了因为我当时还不知道有对顶堆这玩意儿。当然你用其他数据结构来水这题也是可以的,我本来也想这么干。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define dbg(x) cerr<<#x<<" = "<<x<<endl
#define ddbg(x,y) cerr<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
using namespace std;
typedef long long ll;
template<typename T>inline char MIN(T&A,T B){return A>B?A=B,:;}
template<typename T>inline char MAX(T&A,T B){return A<B?A=B,:;}
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=+;
int T,n,THXORZ,x,y,cnt; int main(){//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);
read(T);while(T--){
priority_queue<int> mae;
priority_queue<int,vector<int>,greater<int> > ushiro;
read(THXORZ),read(n);printf("%d %d\n",THXORZ,(n+)>>);
read(x);mae.push(x),ushiro.push(x);printf("%d ",x),cnt=;
for(register int i=;i<=n;++i){
if(i&){
read(y);if(x>y)x^=y^=x^=y;
int mid=mae.top();
if(x<=mid&&y>=mid)mae.push(x),ushiro.push(y);
else if(x<mid&&y<mid)mae.pop(),mae.push(x),mae.push(y),ushiro.push(mae.top());
else ushiro.pop(),ushiro.push(x),ushiro.push(y),mae.push(ushiro.top());
++cnt;if(cnt>)puts(""),cnt=;
printf("%d ",mae.top());
}
else read(x);
}
puts("");
}
return ;
}
poj3784 Running Median[对顶堆]的更多相关文章
- POJ3784 Running Median
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1670 Accepted: 823 Description For th ...
- 【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
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
- POJ3784:Running Median
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=3784 用一个"对顶堆& ...
- poj3784(对顶堆)
题意:多组数据,让你求出1~i(i为奇数&&i<=n)的中位数 思路:首先复杂度必为O(n)或O(nlogn)的(数据范围) 思索,如果题目要求1次中位数,好求!排个序,取a[( ...
- POJ 3784.Running Median
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
- hdu3282 链表或者对顶堆
维护序列的动态中位数 第一次用链表做题..感觉指针指来指去也挺麻烦的.. 本题链表解法就是用数组模拟出一个链表,然后离线输入所有数,排序,按照输入顺序在链表里删除元素,一次性删掉两个,然后中位数指针对 ...
- 【uoj#280】[UTR #2]题目难度提升 对顶堆+STL-set
题目描述 给出 $n$ 个数 $a_1,a_2,...,a_n$ ,将其排为序列 $\{p_i\}$ ,满足 $\{前\ i\ 个数的中位数\}$ 单调不降.求字典序最大的 $\{p_i\}$ . 其 ...
随机推荐
- 利用JS最真实的模拟鼠标点击
为了破解永乐票务登录验证码问题 http://www.228.com.cn/auth/login?logout 当然,打码的过程自然依赖第三方平台,但问题是,哪怕平台给了你需要点击的(相对)坐标.你又 ...
- apt-mirror 校验错误文件处理
apt-mirror是一个用来将Debian或Ubuntu的软件源镜像到本地的工具,这个工具工作得非常好,不过有的时候由于网络问题,会有一些文件的校验是失败的,但apt-mirror并不能发现,等到最 ...
- Proving Equivalences (hdu 2767 强联通缩点)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- c# .net 关于接口实现方式:隐式实现/显式实现!
以前在用到接口时,从来没注意到接口分为隐式实现与显示实现.昨天在浏览博客时看到相关内容,现在根据自己的理解记录一下,方便日后碰到的时候温习温习. 通俗的来讲,“显示接口实现”就是使用接口名称作为方法 ...
- linux下修改tomcat80端口
在这里利用iptables防火墙,将80端口的请求转发到8080端口 在root用户下执行iptales -t nat -A PREROUTING -p tcp --dport 80 -j REDIR ...
- 数据挖掘、目标检测中的cnn和cn---卷积网络和卷积神经网络
content 概述 文字识别系统LeNet-5 简化的LeNet-5系统 卷积神经网络的实现问题 深度神经网路已经在语音识别,图像识别等领域取得前所未有的成功.本人在多年之前也曾接触过神经网络.本系 ...
- mac svn命令行使用入门
本文转载至 http://blog.sina.com.cn/s/blog_6bfa2fc10101euf6.html mac svn命令行使用入门 1. 初始化项目 svn import /Use ...
- 如何学习CCIE
想想自己拖了这么久,也没考试,也没积极去做实验,心里也有怨念,其实一直是方法不对,今天心里产生共鸣,后悔当初太年轻. 转载地址:http://bbs.hh010.com/thread-467553-1 ...
- vue element-ui 自动获取光标
<el-input ref="customerInput"></el-input> this.$refs.mark.$el.querySelector('i ...
- new() 和 make() 的区别
https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/07.2.md