【题目链接】

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. centos 搭建pptp

    很多朋友不会在CENTOS 在安装vpn,因为对于菜鸟来说安装VPN的确是个头晕的事情,特别是安装openvpn,不过安装pptp就稍微简单一点,国内网上有很多安装pptp的教程,但是都很繁杂,我在国 ...

  2. python实现二叉树的遍历以及基本操作

    主要内容: 二叉树遍历(先序.中序.后序.宽度优先遍历)的迭代实现和递归实现: 二叉树的深度,二叉树到叶子节点的所有路径: 首先,先定义二叉树类(python3),代码如下: class TreeNo ...

  3. vue基础---计算属性和侦听器

    [一]计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id="example"> ...

  4. Python orm基础

    ORM 对象映射关系程序. 通过orm将编程语言的对象模型和数据库的关系模型建立映射关系,这样我们在使用编程语言对数据库进行操作的时候可以直接使用编程语言的对象模型进行操作就可以了,而不用直接使用sq ...

  5. 在TWaver的Tree节点上画线

    论坛上有同学提出如何在tree上画引导线,之前我们Flex已经实现此功能,现在最新版的HTML5也将添加此功能.先看看效果:详细的使用方法可以参考我们开发手册中可视化视图组件#Tree引导线一章,下面 ...

  6. xmpp使用经验

    IM 标准协议有XMPP\IMPP\PRIM\SIP(SIMPLE)等,其中XMPP基于XML的协议,具备了很好的扩展性(依靠XML的域名空间)并且可以建立在TLS上使用SASL认证. 1.文件比如图 ...

  7. [不断更新中] 各种错误&&总结

    各种错误 无论感觉多稳都要对拍对拍对拍!!! 不要爆long long 不要爆long long 不要爆long long 不要爆long long 不要爆long long 不要爆long long ...

  8. 在vue项目中使用stylus来实现移动端的1px

    1.目录结构(vue项目,但是并不局限于vue) 2.首先定义一个mixin.styl文件 border-1px($color) position: relative &:after disp ...

  9. codechef营养题 第二弹

    第二弾が始まる! codechef problems 第二弹 一.Backup Functions 题面 One unavoidable problem with running a restaura ...

  10. 兼容IE8 addEventListener、removeEventListener 函数

    //兼容bind函数 if(!Function.prototype.bind){ Function.prototype.bind = function(){ if(typeof this !== 'f ...