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(二分+线段树求顺序对)的更多相关文章

  1. AtCoder Regular Contest 101 (ARC101) D - Median of Medians 二分答案 树状数组

    原文链接https://www.cnblogs.com/zhouzhendong/p/ARC101D.html 题目传送门 - ARC101D 题意 给定一个序列 A . 定义一个序列 A 的中位数为 ...

  2. HDU4614 Vases and Flowers 二分+线段树

    分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...

  3. J - Joseph and Tests Gym - 102020J (二分+线段树)

    题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...

  4. Educational Codeforces Round 61 D 二分 + 线段树

    https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...

  5. 【BZOJ-3110】K大数查询 整体二分 + 线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6265  Solved: 2060[Submit][Sta ...

  6. hdu6070 Dirt Ratio 二分+线段树

    /** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...

  7. K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)

    大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...

  8. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  9. UVA 11983 Weird Advertisement --线段树求矩形问题

    题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...

随机推荐

  1. 进化的Spark, 从DataFrame说起

    进化的Spark, 从DataFrame说起:http://www.tuicool.com/articles/IzeY7zM

  2. C语言高级-结构,共用体,文件,链表

    C语言结构 标准声明方式 struct student{        int age;        char sex;    }; 这个可以在main函数中定义:  struct student ...

  3. python中使用Opencv进行人脸检测

    这两天学习了人脸识别,看了学长写的代码,边看边码边理解搞完了一边,再又是自己靠着理解和记忆硬码了一边,感觉还是很生疏,就只能来写个随笔加深一下印象了. 关于人脸识别,首先需要了解的是级联分类器Casc ...

  4. LINUX 修复relocation error: /lib/tls/libc.so.6

    错误信息: relocation error: /lib/tls/libc.so.6: symbol _dl_tls_get_addr_soft, version GLIBC_PRIVATE not ...

  5. 调用webservices报错 原因是没有导入commons-logging和commons-discovery

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/discovery/to ...

  6. A* 算法详解

    最近刷bin巨的搜索进阶专题,做到一个需要A*算法来解决的题,于是开始学A*算法,十分有用处的算法,虽然看上去听复杂的,但其实原理很容易理解,下面转自一篇文章,讲得真的很好. 转:https://ww ...

  7. Excel Sheet Column Number(STRING-TYPE CONVERTION)

    QUESTION Related to question Excel Sheet Column Title Given a column title as appear in an Excel she ...

  8. ECMAScript5新特性之isFrozen、freeze

    对象被冻结后: 1 不能添加属性. 2 不能删除属性. 3 不能修改属性.(赋值) 4 不能修改属性描述符.(会抛异常) var fruit = { name : '苹果', desc : '红富士' ...

  9. My97DatePicker 日历控件

    My97DatePicker 是一款非常强大的日历控件,使用也非常简单,也能修改源码,牛逼我就不吹了,自己用用看 使用 1.引入 <script language="javascrip ...

  10. python如何查看有哪些模块

    Question: 如何查看正则表达式模块re及其相关函数的意义 1.终端命令行下 python >> import sys >> sys.modules ########## ...