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. 基于JS的文本验证

    1,不能为空 <input type="text" onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert(' ...

  2. 第八章 高级搜索树 (xa4)红黑树:删除

  3. 网络基础相关的知识 socket模块

    1.架构 1.C/S架构:client客户端和server服务器端 优势:能充分发挥pc机的性能 2.B/S架构:browser浏览器和server服务器    隶属于C/S架构 B/S架构  统一了 ...

  4. 1.Two Sum (Array; HashTable)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  5. 删除排序数组中的重复数字 II · Remove Duplicates from Sorted Array II

    重复一次 [抄题]: 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. [思维问题]: [ ...

  6. 使用bcp工具对boost库裁剪

    有些时候,我们需要通过源代码来发布我们的产品,在使用了CI工具之后,一般我们要求每天对源码进行构建,以防止代码不可用了还不自知.如果我们使用了Boost库,我们就需要在构建的过程中将Boost同时构建 ...

  7. MVC仓储类Repository

    接口: using Common; using System; using System.Collections; using System.Collections.Generic; using Sy ...

  8. Mina入门:mina版之HelloWorld[z]

    Mina入门:mina版之HelloWorld [z] 一,前言: 在完成上篇文章<Mina入门:Java NIO框架Mina.Netty.Grizzly简介与对比>之后,我们现在可以正式 ...

  9. Linux ldconfig命令

    一.简介 ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig. ldconfig 命令的用途,主要是在默认搜寻目录(/lib和/u ...

  10. PAT 1048 数字加密(20)(代码+思路)

    1048 数字加密(20)(20 分) 本题要求实现一种数字加密方法.首先固定一个加密用正整数A,对任一正整数B,将其每1位数字与A的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对13取 ...