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] ...
随机推荐
- 吴裕雄 数据挖掘与分析案例实战(4)——python数据处理工具:Pandas
# 导入模块import pandas as pdimport numpy as np # 构造序列gdp1 = pd.Series([2.8,3.01,8.99,8.59,5.18])print(g ...
- DOM 练习
练习一: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- ldd3 源码相关问题参见
参考地址: http://www.verydemo.com/demo_c92_i235635.html http://blog.csdn.net/silvervi/article/details/64 ...
- java 解析命令行参数
下载地址: https://jcenter.bintray.com/org/apache/commons/com.springsource.org.apache.commons.cli/1.2.0/ ...
- HTML5 画图--文字
1:html <div style="margin:0 auto;width:794px;height:1123px"> <canvas id="myC ...
- Fiddler 捕获 nodejs 模拟的http请求
1.设置Fiddler Tools->Options-> Connections Allow remote computers to connect: 2.nodejs 请求有多种 2.1 ...
- Spring配置连接池
---------------------siwuxie095 Spring 配置连接池 1.Spring 配置内置连接 ...
- socket接口详解
1. socket概述 socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信. socket起源于UNIX,在Unix一切 ...
- linux 下 php 安装 Gearman
Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务. 一个Gearman请求的处理过程涉及三个角色: Clien ...
- 【原创】Silverlight之TextBox的LostFocus、GotFocus事件
<TextBox x:Name="txtCount" Width="200" Height="35" GotFocus="t ...