描述


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. 搭建Spring、Spring MVC、Mybatis和Freemarker

    搭建Spring.Spring MVC.Mybatis和Freemarker 1.pom文件 <project xmlns="http://maven.apache.org/POM/4 ...

  2. SQL 编译与重编译

    编译的含义 当SQLSERVER收到任何一个指令,包括查询(query).批处理(batch).存储过程.触发器(trigger) .预编译指令(prepared statement)和动态SQL语句 ...

  3. [python]pep8编码规范

    一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...

  4. 关于iOS上的静态库

    最近再进行项目的真机调试,然后发现了一个天坑.就此研究了一些iOS上的静态库的使用: 首先我们是直接拿一个可以运行的项目来制作静态库的,网上大部分都是先创建静态库然后再写内容,看看我的方法. 1.把子 ...

  5. PIMP模式的理解

    看了[C++程序设计技巧]Pimpl机制 之后,想了半天才理解    // MyClass.h 2: class MyClassImpl; // forward declaration 3: clas ...

  6. 「Windows MFC 」「Edit Control」 控件

    「Windows MFC 」「Edit Control」 控件

  7. 简单的一个makefile

    cpp_obj = $(patsubst %.cpp, %.o, $(wildcard *.cpp)) bin : $(cpp_obj) g++ -o bin $(cpp_obj) .PHONY : ...

  8. ASP.NET缓存 Cache

    缓存介绍 如果每次进入页面的时候都查询数据库生成页面内容的话,如果访问量非常大,则网站性能会非常差,而如果只有第一次访问的时候才查询数据库生成页面内容,以后都直接输出内容,则能提高系统性能,这样无论多 ...

  9. Ubuntu启动项设置——之update-rc.d 命令使用

    http://blog.csdn.net/typ2004/article/details/38712887 apache2.nginx.redis这些服务安装之后,会随开机启动,当这些服务并不需要时, ...

  10. [转]iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法

    参考:http://www.mamicode.com/info-detail-469709.html 一.最普通的视图控制器UIViewContoller 一个普通的视图控制器一般只有模态跳转的功能( ...