POJ 3784 Running Median(动态维护中位数)
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
题目意思:对于n个数,每输入到奇数个数,便求一次中位数。
解题思路:我开始就直接暴力,到了奇数位,sort一下,找出中位数,这样在UVAlive上没有超时,但在poj上是超时的,看了看网上的代码才知道处理这样一个动态的中位数使用堆来维护,而这个堆使用优先队列来模拟,一周内第二次见到优先队列了。维护一个大根堆和一个小根堆,且保证大根堆里的所有数都比小根堆里的所有数小,而且大根堆的大小等于小根堆或者大1,则大根堆堆顶就是中位数。对于新插入的数,与中位数比较决定插入哪个堆中,插入之后维护一下两个堆的大小。每次维护需要进行常数个堆上的操作,所以复杂度O(nlogn)。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> > bh;///从小到大
priority_queue<int,vector<int>,less<int> > sh;///从大到小
int main()
{
int t,n,num;
int i,j,m,p,q,x,y,z,K;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&num,&n);
printf("%d %d\n",num,n/+);
while (!bh.empty())
{
bh.pop();
}
while (!sh.empty())
{
sh.pop();
}
for (i=; i<=n; i++)
{
scanf("%d",&x);
if (sh.empty()||sh.top()>x)
{
sh.push(x);///放入小堆
}
else
{
bh.push(x);///放入大堆
}
if (sh.size()>(i+)/)
{
x=sh.top();
sh.pop();
bh.push(x);
}
if (sh.size()<(i+)/)
{
x=bh.top();
bh.pop();
sh.push(x);
}
if (i%)
{
printf("%d",sh.top());
if (i==n||(i+)%==)
{
printf("\n");
}
else
{
printf(" ");
}
}
}
}
return ;
}
POJ 3784 Running Median(动态维护中位数)的更多相关文章
- POJ 3784 Running Median【维护动态中位数】
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- POJ 3784 Running Median (动态中位数)
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...
- POJ 3784.Running Median
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- POJ 3784 Running Median (模拟水过带翻译)
Description Moscow is hosting a major international conference, which is attended by n scientists fr ...
- 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
[题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...
- Running Median POJ - 3784
本题使用对顶堆做法. 为了动态维护中位数,我们可以建立两个堆 :一个大根对,一个小根堆. 用法:在动态维护的过程中,设当前的长度为length,大根堆存从小到大排名 $1 \thicksim \dfr ...
- POJ3784:Running Median
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=3784 用一个"对顶堆& ...
- 【POJ3784】Running Median
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
随机推荐
- Angular7教程-02-Angular项目目录及基本文件说明
本教程基于Angular7,更新时间2018-11-05. 1. 项目根目录如下: e2e文件夹:end to end,测试目录,主要用于集成测试. node_modules:项目的模块依赖目录. s ...
- js获取今天,明天,本周五,下周五日期的函数
代码比较简单,随便写写 /** * a连接快速选择日期函数 */ function timeChooseSimple(key, me) { //today,tomorrow,thisWeek,next ...
- Windows常用shell命令
一.Windows的Shell命令又是Windows的CMD命令.而cmd命令又是原来MS-DOS系统保留下来 二.Windows Shell命令是基于配置好的Path环境变量,对Shell命令在Pa ...
- ElasticSearch优化系列二:机器设置(内存)
预留一半内存给Lucene使用 一个常见的问题是配置堆太大.你有一个64 GB的机器,觉得JVM内存越大越好,想给Elasticsearch所有64 GB的内存. 当然,内存对于Elasticsear ...
- error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
今天在使用easywechat对接企业打款到银行卡时,遇到了两个错误 error:0906D064:PEM routines:PEM_read_bio:bad base64 decode 和 erro ...
- PHP 变量分页标签页面源代码技术分享
最近在研究PHP的常规变量的分页源代码. 现在发布一个给大家看一下. defined('IN_DUOAO') or exit('No permission resources.');$smarty ...
- ACM1019:Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- android studio 调试技巧(简直太好用)
android studio 调试技巧(简直太好用) 说到android studio的调试,很多人可能会说,这有什么可讲的不就是一个断点调试么,刚开始我也是这么认为的,直到我了解之后,才发现,调试原 ...
- UCSC 工具链接
http://hgdownload.cse.ucsc.edu/admin/exe/linux.x86_64/
- tarjan强连通模板
#include<stdio.h>//用于求一个图存在多少个强连通分量 #include<string.h> #include<vector> using name ...