【题目链接】

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的更多相关文章

  1. 【POJ 3784】 Running Median (对顶堆)

    Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...

  2. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  3. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  4. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  5. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  6. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  7. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  8. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  9. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

随机推荐

  1. 梦想Android版CAD控件2018.10.12更新

    下载地址: http://www.mxdraw.com/ndetail_10106.html 1. 增加读写对象扩展字典功能 2. 修改样条线显示错误 3. 修改shx文字显示错误 4. 增加向量运算 ...

  2. java虚拟机(二)--类加载机制和双亲委派模型

    一.类的生命周期 加载(Loading).验证(Verification).准备(Preparation).解析(Resolution).初始化(Initialization).使用(Using).卸 ...

  3. idea之查看类的上下级继承关系

  4. document.write() 和 document.writeln区别

    document.write() 和 document.writeln 都是JavaScript向客户端写入的方法,writeln是以行方式输出的,但并不是指页面实际效果中的换行,两种方法在查看源代码 ...

  5. 题解 洛谷P3203/BZOJ2002【[HNOI2010]弹飞绵羊】

    裸的LCT,关键是要怎么连边,怎么将这种弹飞关系转化成连边就行了. 那么我们可以这样连边: 一个节点i的爸爸就是i+ki. 没有i+ki那么就被弹飞了,即\(i\)的爸爸是虚拟节点n+1. 那么怎么求 ...

  6. noip模拟赛 都市

    分析:是一道非常有意思的题,30分的暴力的话枚举每个位置是什么数,然后排个序,用map判一下重就好了,比较麻烦. 满分做法显然不可能讨论每个位置所有的情况,肯定是有规律的,现将这n*(n-1)/2个数 ...

  7. 转载 - Struts2拦截器配置

    出处:http://blog.csdn.net/axin66ok/article/details/7321430 目录(?)[-] 理解拦截器 1 什么是拦截器 2 拦截器的实现原理 拦截器的配置 使 ...

  8. JS动态添加div,然后在div中添加元素

    需求: 组织部中有个这样的需求,根据年份动态显示该年份下的定性指标! 我的做法: 先是放一个空的div,让后根据指标的数据,动态的往div中添加元素. 代码: 空的div,存放定性指标 <div ...

  9. 数据库删除数据表重复数据,只留下ID较小的行

    删除表中重复数据,留下ID比较小的行 delete from 表 where [重复字段] in (select [重复字段] from 表 group by 字段 having count([字段] ...

  10. 分享一个灰常好的 dapper 扩展插件: Rainbow

    dapper 是一个效率非常高的orm  框架 ,效率要远远大于 我们大微软的EF .    它只有一个类文件,非常之小.(在 EF 5.0 后 微软已经做了 改进) ps; 由于之前我也没测试过,只 ...