<题目链接>

中位数可以转化为区间第k大问题,当然是选择Treap实现名次树了啊。(笑)

功能十分简单的Treap即能满足需求——只需要插入与查找第大的功能。

插入第i个数时,如果i是奇数,随即询问当前排名第(i+1>>1)的数。

注意是一边插入一边询问,这样可以保留原序列的顺序,而不是所有插入完后再询问。

这样一趟下来,所有数都插入完毕了,询问也处理完毕了。

封装版代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
const int MAXN=100010;
int n;
class Treap
{
public:
Treap(void)
{
rt=cnt=0;
memset(a,0,sizeof a);
memset(s,0,sizeof s);
}
void Insert(int x)
{
_Insert(rt,x);
}
int Xth(int x)
{
return _Xth(rt,x);
}
private:
bool a[MAXN];
int rt,cnt;
struct node
{
int l,r,v,p,size,num;
}s[MAXN];
int Random(void)
{
int x;
while(a[x=rand()%MAXN]);
a[x]=1;
return x;
}
void Update(int i)
{
s[i].size=s[s[i].l].size+s[s[i].r].size+s[i].num;
}
void L_Rotate(int &i)
{
int t=s[i].r;
s[i].r=s[t].l,s[t].l=i;
s[t].size=s[i].size;
Update(i),i=t;
}
void R_Rotate(int &i)
{
int t=s[i].l;
s[i].l=s[t].r,s[t].r=i;
s[t].size=s[i].size;
Update(i),i=t;
}
void _Insert(int &i,int x)
{
if(!i)
{
s[i=++cnt].v=x,s[i].p=Random();
s[i].size=s[i].num=1;
return;
}
++s[i].size;
if(x==s[i].v)
++s[i].num;
else if(x<s[i].v)
{
_Insert(s[i].l,x);
if(s[s[i].l].p>s[i].p)
R_Rotate(i);
}
else
{
_Insert(s[i].r,x);
if(s[s[i].r].p>s[i].p)
L_Rotate(i);
}
}
int _Xth(int i,int x)
{
if(!i)
return 0;
int t;
if(x<=s[s[i].l].size)
return _Xth(s[i].l,x);
else if(x>(t=s[s[i].l].size+s[i].num))
return _Xth(s[i].r,x-t);
else
return s[i].v;
}
}T;
int main(int argc,char *argv[])
{
scanf("%d",&n);
srand((unsigned)time(NULL));
for(int i=1,x;i<=n;++i)
{
scanf("%d",&x);
T.Insert(x);
if(i&1)
printf("%d\n",T.Xth(i+1>>1));
}
return 0;
}

精简版代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
const int MAXN=100010;
bool a[MAXN];
int n,rt,cnt;
struct node
{
int l,r,v,p,size,num;
}s[MAXN];
int Random(void)
{
int x;
while(a[x=rand()%MAXN]);
a[x]=1;
return x;
}
void Update(int i)
{
s[i].size=s[s[i].l].size+s[s[i].r].size+s[i].num;
}
void L_Rotate(int &i)
{
int t=s[i].r;
s[i].r=s[t].l,s[t].l=i;
s[t].size=s[i].size;
Update(i),i=t;
}
void R_Rotate(int &i)
{
int t=s[i].l;
s[i].l=s[t].r,s[t].r=i;
s[t].size=s[i].size;
Update(i),i=t;
}
void Insert(int &i,int x)
{
if(!i)
{
s[i=++cnt].v=x,s[i].p=Random();
s[i].size=s[i].num=1;
return;
}
++s[i].size;
if(x==s[i].v)
++s[i].num;
else if(x<s[i].v)
{
Insert(s[i].l,x);
if(s[s[i].l].p>s[i].p)
R_Rotate(i);
}
else
{
Insert(s[i].r,x);
if(s[s[i].r].p>s[i].p)
L_Rotate(i);
}
}
int Xth(int i,int x)
{
if(!i)
return 0;
int t;
if(x<=s[s[i].l].size)
return Xth(s[i].l,x);
else if(x>(t=s[s[i].l].size+s[i].num))
return Xth(s[i].r,x-t);
else
return s[i].v;
}
int main(int argc,char *argv[])
{
scanf("%d",&n);
srand((unsigned)time(NULL));
for(int i=1,x;i<=n;++i)
{
scanf("%d",&x);
Insert(rt,x);
if(i&1)
printf("%d\n",Xth(rt,i+1>>1));
}
return 0;
}

谢谢阅读。

[Luogu 1168] 中位数的更多相关文章

  1. Luogu P1627 中位数

    Luogu P1627 中位数 先记录目标数的位置,并且把数组映射为: $$a[i]=\begin{cases}-1,a[i]<b\0,a[i]=b\1,a[i]>b\end{cases} ...

  2. [luogu]P1168 中位数[堆]

    [luogu]P1168 中位数 题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数.即前1 ...

  3. LuoGu P1168 中位数

    题目描述 给出一个长度为 $ N $ 的非负整数序列 $ A_i $ ,对于所有 $ 1 ≤ k ≤ (N + 1) / 2 $ ,输出 $ A_1, A_3, -, A_{2k - 1} $ 的中位 ...

  4. luogu P1168 中位数 |树状数组+二分

    题目描述 给出一个长度为NN的非负整数序列A_i,对于所有1 ≤ k ≤ (N + 1) / 21≤k≤(N+1)/2,输出A_1, A_3, -, A_2k - 1的中位数.即前1,3,5,-个数的 ...

  5. 【Luogu P1168】【Luogu P1801&UVA 501】中位数&黑匣子(Black Box)——对顶堆相关

    Luogu P1168 Luogu P1801 UVA 501(洛谷Remote Judge) 前置知识:堆.优先队列STL的使用 对顶堆 是一种在线维护第\(k\)小的算法. 其实就是开两个堆,一个 ...

  6. luogu P3031 [USACO11NOV]高于中位数Above the Median (树状数组优化dp)

    链接:https://www.luogu.org/problemnew/show/P3031 题面: 题目描述 Farmer John has lined up his N (1 <= N &l ...

  7. luogu P1627 [CQOI2009]中位数

    传送门 要求有多少个长度为奇数的区间满足某个数为区间中位数 这样的区间,大于中位数的数个数 等于 小于中位数的数个数 用类似于前缀和的方法,设\(X_i\)为\(i\)和数\(b\)形成的区间内,大于 ...

  8. [LUOGU] P3871 [TJOI2010]中位数

    题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...

  9. [Luogu]中位数

    Description Luogu1168 Solution 一种神奇的做法:开一个大根堆和小根堆,保证大根堆比小根堆多1个元素,且大根堆堆顶元素比小根堆堆顶元素小,那么大根堆堆顶就是中位数.插入的时 ...

随机推荐

  1. DDB与DIB

    DB与DIB的区别是什么?觉得书上介绍的有点抽象.不容易理解.他们两者之间的区别的“物理意义” [“现实意义”]——姑且这么叫吧,呵呵!被这个问题困扰了很久,所以今天决定好好查资料总结一下,把它彻底搞 ...

  2. python中装饰器的原理以及实现,

    python版本 3.6 1.python的装饰器说白了就是闭包函数的一种应用场景,在运用的时候我们遵循 #开放封闭原则:对修改封闭,对拓展开放 2.什么是装饰器 #装饰他人的器具,本身可以是任意可调 ...

  3. The New Day

    于博毅 160809107 爱好电脑研究 选大学专业的时候,把计算机类放在了第一专业,当时从小就很喜欢计算机,以前有接触过编程但仅限于看书,并没有动手实践过,选课的时候看了一下专业课程,都是我想学的 ...

  4. 【alpha】Scrum站立会议第2次....10.17

    小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app 1.任务进度 成员 已完成 今日完成 李权 数据库设计 消息发送代码实现 于淼 注册.登录界面,以及登录界面后台代码.发 ...

  5. PAT 甲级 1032 Sharing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...

  6. 不清楚System.Diagnostics.Process.Start(e.LinkText); 的含义

    using System; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms ...

  7. ICE checkbox 用法

    Hello everybody, I have a datable which contain multiple lines gotten from database, in the header o ...

  8. 【Python】python学习之总结

    迭代器: def gen(): a = yield a a = a * yield a for i in gen(): print(i) 创建一个函数,循环体,yield循环到此就返回一个值.调用函数 ...

  9. hdu3625-Rooms

    题目 有\(n\)个房间,\(n\)个钥匙,每个钥匙随机出现在一个房间里,一个房间里有且仅有一个钥匙.我们现在手上没有钥匙,但我们要搜索所有的房间,所以我们有\(k\)次机会把一个房间炸开.一号房间里 ...

  10. 【bzoj5001】搞事情 暴力

    题目描述 给定一个NM的01矩阵,每次可以选定一个位置,将它和它相邻格子的数取反.问:怎样操作使得所有格子都变为0.当有多组解时,优先取操作次数最小的:当操作次数相同时,优先取字典序最小的. 输入 第 ...