Description

Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numbers: ∣Xi - Xj∣ ( ≤ i < j ≤ N). We can get C(N,) 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/)-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 = .

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 ≤ ,,, ≤ N ≤ ,, )

Output

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

Sample Input


Sample Output


Source

 
这道题直接暴力枚举肯定是超时。
可以采用二分枚举一个数mid。对a数组排序后,与a_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的话,说明mid太大了。以此为条件进行第一重二分搜索,第二重二分搜索是对a的搜索,直接用lower_bound实现。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define N 100006
int n,m;
int a[N];
bool solve(int mid){
int cnt=;
for(int i=;i<n;i++){
int tmp=n-(lower_bound(a,a+n,a[i]+mid)-a);//a[i]加上mid看看有多少个
cnt+=tmp;
}
if(cnt>m) return true;//如果太多,则要调整mid向上,>还是>=有时候要调整
return false;
}
int main()
{
while(scanf("%d",&n)==){
m=n*(n-)/;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
int low=;
int high=a[n-]-a[];
while(low<high){
int mid=(low+high)>>;
if(solve(mid)){
low=mid+;
}
else{
high=mid;
}
}
printf("%d\n",low-);//有时要调整,减1或不减
}
return ;
}

poj 3579 Median (二分搜索之查找第k大的值)的更多相关文章

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

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

  2. 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 ...

  3. 查找第K大的值

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

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

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

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

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

  6. POJ_3579_Median_(二分,查找第k大的值)

    描述 http://poj.org/problem?id=3579 给你一串数,共C(n,2)个差值(绝对值),求差值从大到小排序的中值,偶数向下取. Median Time Limit: 1000M ...

  7. 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 ...

  8. 基于快速排序思想partition查找第K大的数或者第K小的数。

    快速排序 下面是之前实现过的快速排序的代码. function quickSort(a,left,right){ if(left==right)return; let key=partition(a, ...

  9. 如何用快排思想在O(n)内查找第K大元素--极客时间王争《数据结构和算法之美》

    前言 半年前在极客时间订阅了王争的<数据结构和算法之美>,现在决定认真去看看.看到如何用快排思想在O(n)内查找第K大元素这一章节时发现王争对归并和快排的理解非常透彻,讲得也非常好,所以想 ...

随机推荐

  1. python批量下载

    # -*- coding: utf-8 -*-__author__ = 'Administrator'from PyQt4.Qt import *from PyQt4.QtCore import *f ...

  2. AJAX上传文件

    function up_files() { var fileSelect = document.getElementById('file-select'); var files = fileSelec ...

  3. Css实现垂直水平居中的六种方法

    经常在项目中用到,今天总结了一下: 演示地址:http://codepen.io/anon/pen/xGdpOa 以下两个类为公共类,便于更好的显示效果,非核心代码 .common{ width: 6 ...

  4. C++读写CSV文件

    前两天看了<Reading and Writing CSV Files in MFC>(http://www.codeproject.com/Articles/53759/Reading- ...

  5. Chapter 4: Spring and AOP:Spring's AOP Framework -- draft

    Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...

  6. 私人C#笔记

      coust 定义常量 string是密封类,所以不能继承它 namespace默认是按照文件夹的结构命名的,如(System.文件夹.子文件夹),而且namespace是可以手动改的   Arra ...

  7. http status 源码

    private static readonly String[][] s_HTTPStatusDescriptions = new String[][] { null, new String[] { ...

  8. Java 日期与字符串的转换

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  9. linux删除ORACLE【weber出品必属精品】

    关闭数据库 sqlplus / as sysdba shutdown abort 清除oracle软件 su - oracle cd $ORACLE_BASE rm -rf * rm -rf /etc ...

  10. .Net调用Office Com组件的原理及问题检索com类工厂组件检索 COM 类工厂中 CLSID 为 {XXX} 的组件失败

    我是在本地32位操作系统+vs2010+office2007做创建并下载Excel,ppt文件的操作没有问题,发布到64位系统的服务器上报错,最开始报错:: 1:Retrieving the COM ...