题目描述

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

输入输出格式

输入格式:

输入文件median.in的第1行为一个正整数N,表示了序列长度。

第2行包含N个非负整数A[i] (A[i] ≤ 10^9)。

输出格式:

输出文件median.out包含(N + 1) / 2行,第i行为A[1], A[3], …, A[2i – 1]的中位数。

输入输出样例

输入样例#1: 复制

7
1 3 5 7 9 11 6
输出样例#1: 复制

1
3
5
6

说明

对于20%的数据,N ≤ 100;

对于40%的数据,N ≤ 3000;

对于100%的数据,N ≤ 100000。

/*
将a数组去重后存在b数组里,用b数组的大小建树。
用树的l作为数字,num记录这个数出现的次数,
则 更新时,找到a在b数组中的位置,让此位置的数++,表示这个数出现了一次
查询的时候,输出第i/2+1个数,则让x=i/2+1,
如果root的左儿子的num>=x,则说明要找的数在左子树里,否则在右子树里,
如果是去右子树里找,则让x-=tree[root<<1].num,因为我们在右子树里要找的是第x-=tree[root<<1].num个。
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std; const int N=1e5+; int n;
int a[N],b[N];
struct Tree
{
int l,r,mid;
int num;
}tree[N<<]; int read()
{
char c=getchar();int num=;
for(;!isdigit(c);c=getchar());
for(;isdigit(c);c=getchar())
num=num*+c-'';
return num;
} void build(int root,int l,int r)
{
tree[root].l=l,tree[root].r=r,tree[root].mid=l+r>>;
if(l==r)
return;
build(root<<,l,tree[root].mid);
build(root<<|,tree[root].mid+,r);
} void update(int root,int x)
{
++tree[root].num;
if(tree[root].l==tree[root].r)
return;
if(x<=tree[root].mid)
update(root<<,x);
else
update(root<<|,x);
} int query(int root,int num)
{
if(tree[root].l==tree[root].r)
return tree[root].l;
if(num<=tree[root<<].num)
return(query(root<<,num));
else
return(query(root<<|,num-tree[root<<].num));
} int main()
{
n=read();
for(int i=;i<=n;++i)
a[i]=read(),b[i]=a[i];
sort(b+,b+n+);
int bound=unique(b+,b+n+)-b;
build(,,n);
for(int i=;i<=n;++i)
{
int pos=lower_bound(b+,b+bound+,a[i])-b;
update(,pos);
if(i%)
printf("%d\n",b[query(,i/+)]);
}
return ;
}

P1168 中位数(线段树)的更多相关文章

  1. BZOJ 2653 middle (可持久化线段树+中位数+线段树维护最大子序和)

    题意: 左端点在[a,b],右端点在[c,d],求这个线段里中位数(上取整)最大值 思路: 对数组离散化,对每一个值建中位数的可持久化线段树(有重复也没事),就是对于root[i],大于等于i的值为1 ...

  2. 洛谷P1168 中位数——set/线段树

    先上一波链接 https://www.luogu.com.cn/problem/P1168 这道题我们有两种写法 第一种呢是线段树,我们首先需要将原本的数据离散化,线段树维护的信息就是区间内有多少个数 ...

  3. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

  4. 【BZOJ-2653】middle 可持久化线段树 + 二分

    2653: middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1298  Solved: 734[Submit][Status][Discu ...

  5. Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序

    题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s   内存限制:512.0MB    总提交次数:196   AC次数:65   平均分: ...

  6. HDU 5919 Sequence II(可持久化线段树)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...

  7. poj 2010 Moo University - Financial Aid (贪心+线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...

  8. 【BZOJ4071】八邻旁之桥(线段树)

    [BZOJ4071]八邻旁之桥(线段树) 题面 BZOJ权限题,洛谷链接 题解 既然\(k<=2\) 那么,突破口就在这里 分类讨论 ①\(k=1\) 这...不就是中位数吗.... 直接把所有 ...

  9. P1168 中位数

    P1168 中位数树状数组+二分答案.树状数组就是起一个高效查询比二分出来的数小的有几个. #include<iostream> #include<cstdio> #inclu ...

随机推荐

  1. 异常【kubelet cgroup driver:cgroupfs跟docker cgroup driver:systemd不一致】

    修改docker.service vi /lib/systemd/system/docker.service 找到 --exec-opt native.cgroupdriver=systemd \ 修 ...

  2. ASP.NET EF实体主外键关系

    其他解释 https://www.cnblogs.com/wuhenke/archive/2010/08/11/1797307.html 主键.外键 需要删除完外键表才能删除主键表 一对一关系peop ...

  3. 12.1 Mapping手动创建

    只能在index里的field不存在的时候,才能指定新field的数据类型,field有数据后,就不能再修改field的类型了 可创建的类型如下: integer double date text/s ...

  4. openwrt luci web分析

    openwrt luci web分析 来源 https://www.jianshu.com/p/596485f95cf2 www/cbi-bin/luci #!/usr/bin/lua --cgi的执 ...

  5. 【转载】C#中Add方法将往List集合末尾添加相应元素对象

    在C#的List集合操作中,有时候需要将符合条件的对象添加到已有List集合中的末尾,此时就需要使用到List集合的Add方法,Add方法的作用为将对应的元素添加到List集合末尾,Add方法签名为v ...

  6. GitHub上传文件夹

    1.输入自己的用户名和邮箱 为注册GitHub账号时所用的用户名和邮箱;我的用户名为“1997ST2016”,邮箱为“1324971964@qq.com ”. $ git config --globa ...

  7. Vivado debug异常现象

    前言 bit文件和ltx文件的信号位宽不匹配问题.用了dont_touch等属性没用... WARNING: [Labtools 27-1972] Mismatch between the desig ...

  8. LeetCode算法01 Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. scrapy 用pycharm调试

    1. 用pycharm打开scrapy项目,随便右击一个.py文件,选择Debug "***" 2. pycharm 右上角点击刚才debug的文件,选择Edit Configur ...

  10. 尚硅谷韩顺平Linux教程学习笔记

    目录 尚硅谷韩顺平Linux教程学习笔记 写在前面 虚拟机 Linux目录结构 远程登录Linux系统 vi和vim编辑器 关机.重启和用户登录注销 用户管理 实用指令 组管理和权限管理 定时任务调度 ...