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. 【星云测试】开发者测试(3)-采用精准测试工具对springcloud微服务应用进行穿透测试

    1.微服务简介 微服务英文名称Microservice,Microservice架构模式就是将整个Web应用组织为一系列小的Web服务.这些小的Web服务可以独立地编译及部署,并通过各自暴露的API接 ...

  2. i2c子系统

    linux内核的I2C驱动框架总览(1)I2C驱动框架的主要目标是:让驱动开发者可以在内核中方便的添加自己的I2C设备的驱动程序,从而可以更容易的在linux下驱动自己的I2C接口硬件(2)源码中I2 ...

  3. 使用XWAF框架(4)——LunarCalendar日历组件

    XWAF提供了管理日历的com.xwaf.date.LunarCalendar静态类,可以直接使用,非常方便.该类包括六个主要静态方法: 4.1  isLeapYear(int year) 判断公历年 ...

  4. oracle 11G dataguard 恢复

    检查主备机的sys 密码是否一致,忘记密码可以修改,同步 .alter user sys identified by xxx: orapwd file=oraxxx.ora password=admi ...

  5. mariadb或者mysql查看某个库相关的用户授权信息

    mariadb或者mysql查看某个库相关的授权信息 SELECT * FROM mysql.Db WHERE Db='DB_NAME';

  6. vue数组赋值

    在使用vue开发移动端项目过程中,统一数组在对多个变量赋值时:希望一个数组的改变不影响另外一个数组,此时可以使用如下方式实现: let arr = [] let a1 = JSON.parse(JSO ...

  7. 一个数据仓库时代开始--Hive

    一.什么是 Apache Hive? Apache Hive 是一个基于 Hadoop Haused 构建的开源数据仓库系统,我们使用它来查询和分析存储在 Hadoop 文件中的大型数据集.此外,通过 ...

  8. jdbc之Statement和Preparement

    Jdbc DML 操作 Statement:静态SQL操作 每次操作都会将sql语句提交到数据库执行一次,性能比较低 // 1.加载驱动程序 Class.forName(driverName); // ...

  9. 树莓派GPIO控制LED彩灯

    树莓派使用GPIO接口来控制LED灯,自制五彩炫光的节日彩灯. 1.硬件准备 a. 树莓派(Raspberry Pi)一个 b. 彩色RGB二极管 c. 杜邦线 d. 5V电源引脚 以上所有零件均可在 ...

  10. 搭建iSCSI共享IPSAN

    iSCSI(internet SCSI)是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行SCSI协议,使其能够在诸如高速千兆以太网上进行路由选择. ...