【POJ 3784】 Running Median
【题目链接】
http://poj.org/problem?id=3784
【算法】
对顶堆算法
要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆中
每次插入元素时,先将插入元素与小根堆堆顶比较,如果比堆顶小,则插入小根堆,否则,插入大根堆,然后,判断两个堆
的元素个数是否平衡,若不平衡,则交换两个堆的堆顶
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 10010 int i,t,n,tmp,len,T;
int a[MAXN],ans[MAXN]; struct BHeap
{
int tot;
int hp[MAXN];
inline void clear()
{
tot = ;
}
inline void Up(int pos)
{
int fa;
if (pos == ) return;
fa = pos / ;
if (hp[pos] > hp[fa])
{
swap(hp[pos],hp[fa]);
Up(fa);
}
}
inline void Down(int pos)
{
int son;
son = pos * ;
if (son > tot) return;
if (son < tot && hp[son+] > hp[son]) son++;
if (hp[son] > hp[pos])
{
swap(hp[son],hp[pos]);
Down(son);
}
}
inline void Insert(int x)
{
tot++;
hp[tot] = x;
Up(tot);
}
inline void del()
{
swap(hp[],hp[tot]);
tot--;
Down();
}
inline int get()
{
return hp[];
}
} B;
struct SHeap
{
int tot;
int hp[MAXN];
inline void clear()
{
tot = ;
}
inline void Up(int pos)
{
int fa;
if (pos == ) return;
fa = pos / ;
if (hp[pos] < hp[fa])
{
swap(hp[pos],hp[fa]);
Up(fa);
}
}
inline void Down(int pos)
{
int son;
son = pos * ;
if (son > tot) return;
if (son < tot && hp[son+] < hp[son]) son++;
if (hp[son] < hp[pos])
{
swap(hp[son],hp[pos]);
Down(son);
}
}
inline void Insert(int x)
{
tot++;
hp[tot] = x;
Up(tot);
}
inline void del()
{
swap(hp[],hp[tot]);
tot--;
Down();
}
inline int get()
{
return hp[];
}
} S; int main()
{ scanf("%d",&T);
while (T--)
{
len = ;
scanf("%d%d",&t,&n);
S.clear(); B.clear();
for (i = ; i <= n; i++) scanf("%d",&a[i]);
for (i = ; i <= n; i++)
{
if (i == )
{
B.Insert(a[i]);
ans[++len] = a[i];
continue;
}
if (a[i] <= B.get()) B.Insert(a[i]);
else S.Insert(a[i]);
if (B.tot - S.tot >= )
{
tmp = B.get();
B.del();
S.Insert(tmp);
}
if (S.tot - B.tot > )
{
tmp = S.get();
S.del();
B.Insert(tmp);
}
if (i & ) ans[++len] = S.get();
}
printf("%d %d\n",t,len);
for (i = ; i <= len; i++)
{
if (i % == ) printf("%d",ans[i]);
else if (i % == ) printf(" %d\n",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
} return ; }
【POJ 3784】 Running Median的更多相关文章
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- 了解固态硬盘SSD,竟然如此简单!小白也能懂!
https://www.youtube.com/watch?v=alb6-zp52mA
- Git 分支使用
一个主分支肯定是不够用的,不同的开发最好放在不同的分支上,在最后进行合并,不然在开发中会相互干扰. PS:环境Window xp,Git-1.8.4-preview20130916(http://gi ...
- php第二十五节课
详情删除 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- 利用CMD 創建新文件的機種方法
用 CMD 創建新文件 説明一下: 是在Windows的 CMD命令行模式下,或者在PowerShell命令行模式下創建新文件的機種方法. 創建空文件 cd.>a.txt cd.表示改变当前目录 ...
- idea+maven配置log4j详解
经过上一篇的讲解,知道了实现log4j打印日志依赖的jar包共3个,在pom.xml中加入相关依赖: <!-- 添加log4j日志相关jar包:共3个jar--> <!-- http ...
- LINUX-JPS工具
JPS工具 jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/u ...
- Datatable 插入一行数据到第一行
var t = $('#passwdHOST').DataTable({ 'searching': true, 'ordering': false, 'autoWidth': false, dom: ...
- STM32 配置PC13~PC15
在STM32的数据手册的管脚分配图中可以看到:PC14与OSC32_IN公用一个引脚,PC15与OSC32_OUT公用一个引脚,它们的使用方法如下: 当LSE(低速外部时钟信号)开启时,这两个公用管脚 ...
- orcad中注意的事情
1.地的标识不能放到已经分配了网络的线上. 在用orcad画原理图的时候,把电源放到网络的时候需要特别的注意,如果,将电源地直接放到线上,而这根线又已经被分配了网络标号,那这个地会随已经分配了的网络号 ...
- RequestMapping_HiddenHttpMethodFilter 过滤器
[REST] 1.REST:即Representational State Transfer.(资源)表现层状态转化.是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以得 ...