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\}$ . 其 ...
随机推荐
- U盘EFI分区删不掉怎么办
方法/步骤 将U盘查到电脑上 点击[开始]找到并打开[Windows系统]的下拉按钮,找到[命令提示符] 在“命令提示符”上右键>[更多]>[以管理员身份运行]打开“管理员:命令提示符”窗 ...
- modelsim-altera IP核仿真
modelsim 仿真fifo时出现 Instantiation of 'scfifo' failed. The design unit was not found. 2012-07-21 13:27 ...
- 一个数组nums,其中任意两个值等于给定值target,返回这两个值在nums里的位置
package com.java.tencent; import java.lang.reflect.Array; import java.util.Arrays; import java.util. ...
- 数字证书中keytool命令使用说明
这个命令一般在JDK\jre\lib\security\目录下操作 keytool常用命令 -alias 产生别名 -keystore 指定密钥库的名称(就像数据库一样的证书库,可以 ...
- linux SPI驱动——spi协议(一)
一:SPI简介以及应用 SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在 ...
- 【解决】无法连接 MKS:套接字连接尝试次数太多正在放弃
https://blog.csdn.net/wjunsing/article/details/78496224 我的电脑 -> 右键 -> 管理 -> 服务和应用程序 -> 服 ...
- Linux - 命令行 管道(Pipelines) 具体解释
命令行 管道(Pipelines) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24249529 管道操作符" ...
- vs中使用M_PI的问题及解决 角度转弧度&根据弧度计算圆周上点的坐标的方法
M_PI 是一个宏定义,圆周率的定义 C/C++ code #define M_PI 3.14159265358979323846 此宏定义和编译器有关,TC中M_PI宏就定义在& ...
- 【BZOJ2789】[Poi2012]Letters 树状数组
[BZOJ2789][Poi2012]Letters Description 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符 ...
- 修改linux的hostname (修改linux系统的IP和hostname)
# vi /etc/sysconfig/networkNETWORKING=yesHOSTNAME=yourname //在这修改hostnameNISDOMAIN=eng-cn.platform.c ...