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

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

 

【题意】

给你n个数,然后求它们两两相减的绝对值,然后找出这些绝对值的中位数

【分析】

1、先对n个数排序,那么最后的结果ans一定满足0<=ans<an-a1

2、我们用二分进行逼近。l=0,r=an-a1,mid=(l+r)/2;

3、很明显是原数组两个数相减的绝对值<mid个数,和>mid的个数进行比较。(绝对值的个数<mid,ans在[mid,r]区间,否则在[l,mid]区间内)

4、以求一个数减去a[1]的值小于mid的个数为例,我们找到一个a[i]>=a[1]+mid时最小的一个i,那么数组[1,i)值减去a[1],都小于mid,这样枚举i就可以求得两数相减差值(的绝对值)小于mid的个数。

【代码】

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
inline int read(){
register char ch=getchar();register int x=0;
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x;
}
const int N=2e5+5;
int n,a[N];ll m;
inline bool check(int now){
ll sum=0;
for(int i=1;i<=n;i++)
sum+=lower_bound(a+i+1,a+n+1,a[i]+now)-(a+i)-1;
return m&1?sum<=m/2:sum<m/2;
}
int main(){
while(scanf("%d",&n)==1){
for(int i=1;i<=n;i++) a[i]=read();
sort(a+1,a+n+1);m=1LL*n*(n-1)>>1;
int l=0,r=a[n]-a[1],mid,ans=0;
while(l<=r){
mid=l+r>>1;
if(check(mid)){
ans=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
printf("%d\n",ans);
}
return 0;
}

POJ 3579 Median(二分答案)的更多相关文章

  1. POJ 3579 Median 二分加判断

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

  2. POJ 3579 Median (二分)

                                                                                                         ...

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

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

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

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

  5. POJ 3579 Median 【二分答案】

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

  6. POJ 3104 Drying(二分答案)

    题目链接:http://poj.org/problem?id=3104                                                                  ...

  7. POJ 3122 Pie 二分答案

    题意:给你n个派,每个派都是高为一的圆柱体,把它等分成f份,每份的最大体积是多少. 思路: 明显的二分答案题-- 注意π的取值- 3.14159265359 这样才能AC,,, //By Sirius ...

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

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

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

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

随机推荐

  1. 分页功能实现之通过ajax实现表单内容刷新

    拿代码来说话 我们的需求就是点击翻页功能,实现表格内容局部刷新且能够翻到对应的页面上,不明白? 那么就看看下面的图,需要达到的效果如下所示: 现在要实现的功能就是把红线框起来的表单内容 在点击翻页的时 ...

  2. ubuntu:如何制作类似jeso的系统?

    chroot 下载ubuntu的core包或base包 chroo后,先安装grub,再kernel,基本就ok了! 提示:mount --bind /proc  newroot/proc 可能的问题 ...

  3. 详解CorelDRAW中关于群组的操作

    CorelDRAW软件中的“群组”功能键主要用于整合多个对象.在进行比较复杂的绘图编辑时,通常会有很多的图形对象,为了方便操作,可以对一些对象设定群组.设定群组以后的多个对象,将被看作一个单独的对象. ...

  4. 设计模式之初识IoC/DI(六)

    本篇和大家一起学习IoC和DI即控制反转和依赖注入. 当然听上去这词语非常的专业,真不知道是怎么组出来的,看上去难归看上去难,但稍微理解一下也就这么回事了. 首先我们要明白IoC/DI干嘛用的,不然别 ...

  5. IE每次关闭都提示IE已停止工作

    方法一:打开IE浏览器,点击工具,选择下拉菜单中的internet选项,切换至高级选项卡标签,找到“启用第三方浏览器扩展”选项.把前面的打勾去掉: 方法二:打开IE浏览器,不管是6.0还是更高的版本这 ...

  6. eclipse 搜索 正则表达式

    1.换行搜索,如下: \.dyform\([\r]*[\s]*\{

  7. Golang 在 Mac、Linux、Windows 交叉编译

    https://blog.csdn.net/panshiqu/article/details/53788067

  8. 2、一、Introduction(入门):1、Application Fundamentals(应用程序基础)

    一.Introduction(入门) 1.Application Fundamentals(应用程序基础) Android apps are written in the Java programmi ...

  9. ios开发之--打印bool值

    eg:NSLog(@"Hello,objective-c!");   @表示应该当作NSString字符串来处理. NSLog相当于C语言中的printf,常用于文字输出 NSLo ...

  10. BootStrap Table显示行号,并且分页后依然递增

    bootStrap table 此处使用的是V1.9.0.在网上百度的方法是: { title: '序号', field: '', formatter: function (value, row, i ...