AtCoder - 4351 Median of Medians(二分+线段树求顺序对)
D - Median of Medians
Time limit : 2sec / Memory limit : 1024MB
Score : 700 points
Problem Statement
We will define the median of a sequence b of length M, as follows:
Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M⁄2+1)-th element of b' is the median of b. Here, ⁄ is integer division, rounding down.
For example, the median of (10,30,20) is 20; the median of (10,30,20,40) is 30; the median of (10,10,10,20,30) is 10.
Snuke comes up with the following problem.
You are given a sequence a of length N. For each pair (l,r) (1≤l≤r≤N), let ml,r be the median of the contiguous subsequence (al,al+1,…,ar) of a. We will list ml,r for all pairs (l,r) to create a new sequence m. Find the median of m.
Constraints
1≤N≤105
ai is an integer.
1≤ai≤109
Input
Input is given from Standard Input in the following format:
N
a1 a2 … aN
Output
Print the median of m.
Sample Input 1
Copy
3
10 30 20
Sample Output 1
Copy
30
The median of each contiguous subsequence of a is as follows:
The median of (10) is 10.
The median of (30) is 30.
The median of (20) is 20.
The median of (10,30) is 30.
The median of (30,20) is 30.
The median of (10,30,20) is 20.
Thus, m=(10,30,20,30,30,20) and the median of m is 30.
Sample Input 2
Copy
1
10
Sample Output 2
Copy
10
Sample Input 3
Copy
10
5 9 5 9 8 9 3 5 4 3
Sample Output 3
Copy
8
题意:给出一个序列,求每个子区间中中位数的中位数
题解:
首先如果A过牛客第一场提高组集训T1的神仙们肯定会知道前缀和+二分搞所有区间中位数的玩法
大致操作如下:
如果一个数比mid大,把它赋值为-1否则为1
对这个数组来个前缀和
此时如果某个区间的和为正数说明这个区间的中位数比mid小
然后问题转化成如何求现在有多少个这个区间
显然对前缀和求个顺序对就可以啦
接着就是二分答案
复杂度nlognlogai
代码如下:
#include<bits/stdc++.h>
#define lson root<<1
#define rson root<<1|1
using namespace std; int n;
int a[];
int sum[]; struct node
{
int l,r,sum;
}tr[]; int push_up(int root)
{
tr[root].sum=tr[lson].sum+tr[rson].sum;
} int build(int root,int l,int r)
{
if(l==r)
{
tr[root].sum=;
tr[root].l=l;
tr[root].r=r;
return ;
}
tr[root].l=l;
tr[root].r=r;
int mid=(l+r)>>;
build(lson,l,mid);
build(rson,mid+,r);
push_up(root);
} int update(int root,int pos)
{
if(pos==tr[root].l&&pos==tr[root].r)
{
tr[root].sum++;
return ;
}
int mid=(tr[root].l+tr[root].r)>>;
if(pos<=mid)
{
update(lson,pos);
}
else
{
update(rson,pos);
}
push_up(root);
} int query(int root,int l,int r)
{
if(l>r) return ;
if(l<=tr[root].l&&tr[root].r<=r) return tr[root].sum;
int mid=(tr[root].l+tr[root].r)>>;
if(r<=mid)
{
return query(lson,l,r);
}
else
{
if(l>mid)
{
return query(rson,l,r);
}
else
{
return query(lson,l,mid)+query(rson,mid+,r);
}
}
} int check(int x)
{
long long ans=;
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
{
sum[i]=sum[i-]+((a[i]<=x)?:-);
}
for(int i=;i<=n;i++)
{
sum[i]+=n+;
}
build(,,);
for(int i=;i<=n;i++)
{
ans+=query(,,sum[i]-);
update(,sum[i]);
}
return ans>=1ll*n*(n+)/+;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
int l=,r=1e9,mid;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid))
{
r=mid;
}
else
{
l=mid+;
}
if(r-l<=)
{
mid=check(l)?l:r;
break;
}
}
printf("%d\n",mid);
}
AtCoder - 4351 Median of Medians(二分+线段树求顺序对)的更多相关文章
- AtCoder Regular Contest 101 (ARC101) D - Median of Medians 二分答案 树状数组
原文链接https://www.cnblogs.com/zhouzhendong/p/ARC101D.html 题目传送门 - ARC101D 题意 给定一个序列 A . 定义一个序列 A 的中位数为 ...
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- J - Joseph and Tests Gym - 102020J (二分+线段树)
题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...
- Educational Codeforces Round 61 D 二分 + 线段树
https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)
大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- UVA 11983 Weird Advertisement --线段树求矩形问题
题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...
随机推荐
- pyorient
简介 pyorient是orientdb的python库 该库提供两种访问orientdb的方式:1.client 的方式 2.ogm 的方式(类似于ORM) 由于OGM 封装了client,且由于O ...
- Oracle VM VirtualBox做好虚拟硬盘后,如何进一步修改虚拟硬盘的大小
以管理员身份打开, 命令提示符窗口,然后利用命令cd进入Oracle VM VirtualBox安装目录,如下图: 我进入了Oracle VM VirtualBox安装目录:D:\Program Fi ...
- tensor flow 安装
http://blog.csdn.net/nxcxl88/article/details/52704877?locationNum=13 安装后,一定要运行这句话后 $ source activa ...
- ingress 密码验证
traefik ingress 上面的方式需要引入haprox或者nginx,多引入了一个代理转发层,其实ingress本身就提供了basic auth的支持,在ingress规则中添加额外的认证an ...
- exception keynote
[exception keynote] Note that the parentheses around this tuple are required, because except ValueEr ...
- 常用的SQL语句(牢记)
上课时的重要内容,其中表 t_hq, t_hq2, 以及字段的名字是举例说明. update t_hq t set t.bumendh = '10086';commit;全表更新电话,commit是提 ...
- Metropolis(多源点最短路)
Metropolis https://www.nowcoder.com/acm/contest/203/I 题目描述 魔方国有n座城市,编号为.城市之间通过n-1条无向道路连接,形成一个树形结构. 在 ...
- 9-n个人中选k个人的选择方法种类
用递归法计算从n个人中选择k个人组成一个委员会的不同组合数分析: 1.如果k>n,结果为0 2.k=n时,只有1组 3.k<n的时候,可以把解空间分为两部分:假设其中一个人叫X,那么选X的 ...
- ecplise导入工程出现乱码的解决方案
eclipse之所以会出现乱码问题是因为eclipse编辑器选择的编码规则是可变的.一般默认都是UTF-8或者GBK,当从外部导入的一个工程时,如果该工程的编码方式与eclipse中设置的编码方式不同 ...
- Linux下搭建gtk+2.0开发环境
1.执行如下命令,检查系统是否已安装gtk+ pkg-config --list-all |grep gtk 若命令提示如下,则系统已安装gtk+,否则未安装. 2.若未安装,则执行如下命令进行安装 ...