Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12453   Accepted: 4357

Description

Given N numbers, X1X2, ... , XN, let us calculate the difference of every pair of numbers: ∣Xi - Xj∣ (1 ≤ i  j  N). We can get C(N,2) differences through this work, and now your task is to find the median of the differences as quickly as you can!

Note in this problem, the median is defined as the (m/2)-th  smallest number if m,the amount of the differences, is even. For example, you have to find the third smallest one in the case of = 6.

Input

The input consists of several test cases.
In each test case, N will be given in the first line. Then N numbers are given, representing X1X2, ... , XN, ( X≤ 1,000,000,000  3 ≤ N ≤ 1,00,000 )

Output

For each test case, output the median in a separate line.

Sample Input

4
1 3 2 4
3
1 10 2

Sample Output

1
8

Source

 
二分答案+O(nlogn)判断合法性
 
O(n^2)是明显不行的。
二分枚举中位数,在判断时候枚举每一个arr[i] 作为左端点,然后使用 upper_bound 求出第一个大于 arr[i]+mid 的元素的下标j。
(j-1)-i 就是以arr[i]作为较小的数,可能的方案数。这样判断合法性的复杂度就是 O(nlogn)。
 
btw,这里不光可以求出中位数,其实可以求出任意的第m个数。
 
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long const int maxn = 1e5+;
int n;
ll m;
int arr[maxn];
bool valid(int mid){
int cnt = ;
for(int i=;i<n;i++){
cnt += (upper_bound(arr+i,arr+n,arr[i]+mid)--(arr+i));
}
return cnt >= m;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++)
scanf("%d",&arr[i]);
sort(arr,arr+n);
m = 1ll*n*(n-)/;
if(m%==){
m = m/;
}else m = m/+;
int l = -, r = arr[n-]-arr[];
while(l <= r){
int mid = (l+r)/;
if(valid(mid))
r = mid-;
else
l = mid+;
}
printf("%d\n",l);
}
return ;
}

POJ 3579 Median 二分加判断的更多相关文章

  1. POJ 3579 Median (二分)

                                                                                                         ...

  2. poj 3579 Median 二分套二分 或 二分加尺取

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5118   Accepted: 1641 Descriptio ...

  3. POJ 3579 Median(二分答案+Two pointers)

    [题目链接] http://poj.org/problem?id=3579 [题目大意] 给出一个数列,求两两差值绝对值的中位数. [题解] 因为如果直接计算中位数的话,数量过于庞大,难以有效计算, ...

  4. POJ 3579 Median(二分答案)

    Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...

  5. POJ 3579 Median 【二分答案】

    <题目链接> 题目大意: 给出 N个数,对于存有每两个数的差值的序列求中位数,如果这个序列长度为偶数个元素,就取中间偏小的作为中位数. 解题分析: 由于本题n达到了1e5,所以将这些数之间 ...

  6. poj 3579 Median (二分搜索之查找第k大的值)

    Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...

  7. POJ 3579 median 二分搜索,中位数 难度:3

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3866   Accepted: 1130 Descriptio ...

  8. POJ 3579 二分

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7687   Accepted: 2637 Descriptio ...

  9. POJ 3579 3685(二分-查找第k大的值)

    POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...

随机推荐

  1. What to do next to activate this settings for already existing users

    Link: http://sharepoint.stackexchange.com/questions/89284/sharepoint-2013-mysite-increase-quota Cent ...

  2. Java并发编程(二)创建线程的三种方法

    进程与线程 1.  进程 进程和代码之间的关系就像音乐和乐谱之间的关系一样,演奏结束的时候音乐就不存在了但乐谱还在:程序执行结束的时候进程就消失了但代码还在,而计算机就是代码的演奏家. 2. 线程 线 ...

  3. SQL分页过多时, 如何优化

    问题: 我们经常会使用到分页操作,这里有个问题,在偏移量非常大的时候,它会导致MySQL扫描大量不需要的行然后再抛弃掉.如: , ; 上述这条SQL语句需要查询10020条记录然后只返回最后20条.前 ...

  4. Scala 语法基础

    一 简介 Scala 是一门多范式(multi-paradigm)的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性.Scala 运行在Java虚拟机上,并兼容现有的Java程序.Scal ...

  5. cannot find module 'xxx' 解决办法

    先将node_module文件夹删掉 工程目录下输入命令:npm clean cache 查看package.json里有没有依赖项,有的话npm install 没有就npm install exp ...

  6. Easy-UI中datebox的默认显示当前日期的最简单的两种方法

    在中有一个Today按钮就是实现显示当前日期,所以我们在src/jquery.datebox.js文件中可以找到currentText:'Today'.所以我们可以使用'currentText'和'T ...

  7. 大数据学习之Hadoop快速入门

    1.Hadoop生态概况 Hadoop是一个由Apache基金会所开发的分布式系统集成架构,用户可以在不了解分布式底层细节情况下,开发分布式程序,充分利用集群的威力来进行高速运算与存储,具有可靠.高效 ...

  8. Vim Go开发环境搭建

    基本搭建流程参考了网上的博文以及Vimgo的Github主页 博文https://www.cnblogs.com/breg/p/5386365.html Vim-go主页(我能不能加入项目,做点贡献呢 ...

  9. 常见的Content-Type类型

    Content-Type说明 MediaType,即是Internet Media Type,互联网媒体类型:也叫做MIME类型, 在Http协议消息头中,使用Content-Type来表示具体请求中 ...

  10. poj 2739 Sum of Consecutive Prime Numbers 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K Description Some positive integers can be represented by a ...