描述


http://poj.org/problem?id=3579

给你一串数,共C(n,2)个差值(绝对值),求差值从大到小排序的中值,偶数向下取.

Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5468   Accepted: 1762

Description

Given N numbers, X1, X2, ... , 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 m = 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 X1, X2, ... , XN, ( Xi ≤ 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

分析


可以先把数排序,然后下界0,上界a[n]-a[1],二分假定中值d,如果所有差值中大于等于d的小于等于N/2,说明d太大了.判断d是否可行时如果枚举差值就太慢了,可以对于每一个数x,找所有满足xi>=x+d(xi>x)的xi的个数,这里还是用二分,直接lower_bound即可.

注意:

1.差值共有N=C(n,2)=n*(n-1)/2而不是n.

2.数据范围并不会超int.

 #include<cstdio>
#include<algorithm>
using std :: sort;
using std :: lower_bound; const int maxn=;
int n,N;
int a[maxn]; bool C(int d)
{
int cnt=;
for(int i=;i<n;i++) cnt+=a+n-(lower_bound(a+i+,a+n+,a[i]+d)-);
return cnt<=N/;
} void solve()
{
sort(a+,a+n+);
int l=,r=a[n]-a[];
while(l<r)
{
int m=l+(r-l+)/;
if(C(m)) r=m-;
else l=m;
}
printf("%d\n",l);
} void init()
{
while(scanf("%llu",&n)==)
{
N=n*(n-)/;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
solve();
}
} int main()
{
init();
return ;
}

POJ_3579_Median_(二分,查找第k大的值)的更多相关文章

  1. POJ_3685_Matrix_(二分,查找第k大的值)

    描述 http://poj.org/problem?id=3685 一个n*n的矩阵,(i,j)的值为i*i+100000*i+j*j-100000*j+i*j,求第m小的值. Matrix Time ...

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

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

  3. 查找第K大的值

    这种题一般是给定N个数,然后N个数之间通过某种计算得到了新的数列,求这新的数列的第K大的值 POJ3579 题意: 用$N$个数的序列$x[i]$,生成一个新序列$b$. 新的序列定义为:对于任意的$ ...

  4. poj 3685 Matrix(二分搜索之查找第k大的值)

    Description Given a N × N matrix A, whose element × i + j2 - × j + i × j, you are to find the M-th s ...

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

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

  6. poj 2579 中位数问题 查找第K大的值

    题意:对列数X计算∣Xi – Xj∣组成新数列的中位数. 思路:双重二分搜索 对x排序 如果某数大于 mid+xi 说明在mid后面,这些数的个数小于 n/2 的话说明这个中位数 mid 太大 反之太 ...

  7. hihoCoder 1133 二分·二分查找之k小数(TOP K算法)

    #1133 : 二分·二分查找之k小数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回里我们知道Nettle在玩<艦これ>,Nettle的镇守府有很 ...

  8. 利用划分树求解整数区间内第K大的值

    如何快速求出(在log2n的时间复杂度内)整数区间[x,y]中第k大的值(x<=k<=y)? 其实我刚开始想的是用快排来查找,但是其实这样是不行的,因为会破坏原序列,就算另外一个数组来存储 ...

  9. hiho week 37 P1 : 二分·二分查找之k小数

    P1 : 二分·二分查找之k小数 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回里我们知道Nettle在玩&l ...

随机推荐

  1. ASP.NET页面生命周期总结(1)

    图解:1) 浏览器   :把用户的操作封装成一个请求通过socket发送到后台服务器. 后台服务器:首先有个内核模块Http.sys 和针对每个应用程序池都有一个请求队列.然后请求到达http.sys ...

  2. bootstrap 正则表达式

    <asp:TextBox runat="server"  title="邮箱正确格式:xxx@xxx.xxx" class="form-cont ...

  3. linux管理网络连接指令

    ethtool eth0  查看 eth0对应网卡的设置 ethtool -i eth0 查看 eth0网卡的驱动信息 ethtool -S eth0 查看网卡的统计信息 ethtool -s eth ...

  4. IOS 学习笔记 2015-03-18

    Objective--C 一 关键字 1 KVC 动态设值,动态取值,类似雨java中的反射,而且私有的照样可以设置与获取 2 二 函数 1 retain 给对象引用计数器 + 1 2 release ...

  5. fedora 20 install skype

    http://www.if-not-true-then-false.com/2012/install-skype-on-fedora-centos-red-hat-rhel-scientific-li ...

  6. git 401 错误

    错误信息:error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.n ...

  7. SQL Server 扩展事件(Extented Events)从入门到进阶(1)——从SQL Trace到Extented Events

    由于工作需要,决定深入研究SQL Server的扩展事件(Extended Events/xEvents),经过资料搜索,发现国外大牛的系列文章,作为“学习”阶段,我先翻译这系列文章,后续在工作中的心 ...

  8. 编程书籍分享--pdf

    作为程序员,我觉得我们应该多学习.多思考.多分享. 今天就花费了一点时间把这几年搜集的编程资料上传到了网上做个分享, 其中涵盖.net .java.js.html5.css3.mysql.sqlser ...

  9. ubuntu中vi在编辑状态下方向键不能用的解决

    ubuntu中vi在编辑状态下方向键不能用,还有回格键不能删除等,我们平时习惯的一些键都不能使用. 解决办法: 可以安装vim full版本,在full版本下键盘正常,安装好后同样使用vi命令. 安装 ...

  10. C#中判断bool 类型 代码的最短写法

    看到一个关于写最短代码的,  是一个bool类型判断的:    public bool IsNull(object val) { if (val == null) { return true; } e ...