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\}$ . 其 ...
随机推荐
- jsp获取web.xml 里的配置项
ServletContext servletContext = request.getSession().getServletContext(); String titl ...
- jdbc 链接池的优化
package cn.itcast.jdbc.datasourse; import java.sql.Connection;import java.sql.DriverManager;import j ...
- hadoop权威指南学习
通常情况下,处理少量的大型文件更容易.更有效,为什么呢? map阶段中的键如果不需要可以忽略掉? MapReduce过程也可以用于本地文件的处理,但是如果是要使用到集群的话还需要HDFS. Data ...
- difference between VM, Docker and Vagrant区别
VM: VirtualBox, VMware Docker Vagrant using which you can create VMs or container. It interacts wit ...
- 阿里云Opensearch数据类型
阿里云主要支持以下数据类型,详情参考:https://help.aliyun.com/document_detail/29121.html 类型 说明 INT int64整型 INT_ARRAY in ...
- WCF配置心得
根据蒋金楠老师的博文所说的, WCF的终结点有三个要素组成,分别是地址(Address).绑定(Binding)和契约(Contract),简记可写成Endpoint = ABC. 地址:地址决定了服 ...
- HTML5,不仅仅是看上去非常美(第二弹:打造最美3D机房)
前言 近期项目开发任务告一段落,刚好有时间整理这大半年的一些成果.使用html5时间还不久,对js的认识还不够深入.没办法,曾经一直搞java,对js的一些语言特性和概念一时还转换只是来. 上一篇第一 ...
- ios何时使用self.
本文转载至 http://blog.csdn.net/lvxiangan/article/details/27204265 何时使用self.在网上搜索或者论坛里的回复大多都是简简单单的说这与 ...
- GS给客户单发包以及m_queGcWait(所有GC共享)
GS给客户单发包以及m_queGcWait(所有GC共享) send_stat BaseChannel::SendCmd(int nCmd, void* pData, int nLen) { Prot ...
- Java 8 default 函数
我们知道在java8之前 ,一个类实现一个接口需要实现接口所有的方法, 但是这样会导致一个问题,当一个接口有很多的实现类的时候,修改这个接口就变成了一个非常麻烦的事,需要修改这个接口的所有实现类 不过 ...