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



题目大意:给N个数,两两相减有m=C(N,2)种结果,找出(m/2)-th小的差。

思路:两次二分,第一次二分找那个差数。第二次算出左边有多少比他小的数能够使它们的差小于第一次二分时的数
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*; public class Main {
static int n;
static int a[] = new int[100000];
static int m;
static int solve(int x,int y,int val,int flag){
while(x<=y){
int mid=(x+y)/2;
if(val-a[mid]<=flag)
y=mid-1;
else
x=mid+1; }
return y;
}
static boolean ok(int val){
int j,k=0;
for(int i=2;i<=n;i++){
j=solve(1,i-1,a[i],val);
k+=i-j-1;
}
if(k<m)
return false;
else
return true;
}
public static void main(String[] args) throws IOException {
//Scanner scan = new Scanner(System.in);
StreamTokenizer st = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (st.nextToken()!=StreamTokenizer.TT_EOF) {
n =(int)st.nval;
if(n*(n-1)/2%2!=0){
m=(n*(n-1)/2+1)/2;
}
else
m=n*(n-1)/2/2; for (int i = 1; i <=n; i++) {
st.nextToken();
a[i] = (int)st.nval;
}
Arrays.sort(a,1,n+1);
int l = 0, r = a[n]-a[1];
while(l<=r){
int mid=(l+r)/2;
if(ok(mid))
r=mid-1;
else
l=mid+1;
}
out.println(l);
out.flush();
}
} }

版权声明:本文博主原创文章,博客,未经同意不得转载。

POJ 3579- Median的更多相关文章

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

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

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

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

  3. POJ 3579 Median(二分答案)

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

  4. POJ 3579 Median 二分加判断

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

  5. POJ 3579 Median (二分)

                                                                                                         ...

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

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

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

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

  8. POJ 3579 Median 【二分答案】

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

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

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

  10. 【POJ - 3579 】Median(二分)

    Median Descriptions 给N数字, X1, X2, ... , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤N). 我们能得到 C(N,2) 个 ...

随机推荐

  1. adt-bundle-windows-x86_32-20140702

    adt-bundle-windows-x86_32-20140702 Windows > Preference,在Android里没有NDK选项 法一:help->install new ...

  2. 十分钟搞懂什么是CGI(转)

    原文:CGI Made Really Easy,在翻译的过程中,我增加了一些我在学习过程中找到的更合适的资料,和自己的一些理解.不能算是严格的翻译文章,应该算是我的看这篇文章的过程的随笔吧. CGI真 ...

  3. java反射中Method类invoke方法的使用方法

    package com.zsw.test; import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetExcep ...

  4. SessionFactory的创建和Session的获得

    1.当我们调用 Configuration config=new Configuration().configure(); 时候Hibernate会自己主动在当前的CLASSPATH中搜寻hibern ...

  5. 通过openssh远程登录时的延迟问题解决

    Linux下的ssh 服务器一般用的都是open-ssh,可是发现有些时候通过ssh连接服务器时总会有大概10秒钟左右的延迟. 一开始以为是openssh的安全策略,防止端口扫描,后来发现自己想多了. ...

  6. tshark命令行的使用(转)

    tshark是wireshark的一个命令行工具用于抓包分析: 主要参数如下: 1. 抓包接口类 -i 设置抓包的网络接口,不设置则默认为第一个非自环接口. -D 列出当前存在的网络接口.在不了解OS ...

  7. 【Android进阶】Android面试题目整理与讲解(一)

    这一篇文章专门整理一下研究过的Android面试题,内容会随着学习不断的增加,如果答案有错误,希望大家可以指正 1.简述Activity的生命周期 当Activity开始启动的时候,首先调用onCre ...

  8. 修改easyui datebox默认日期格式

    问题描述: 根据jquery easyui datebox demo中给的示例,导入和使用datebox, 发现日期格式为: 6/22/2011, 其他的今天和关闭也是 Today, Close, 对 ...

  9. Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor

    1.错误描写叙述 信息: Setting autowire strategy to name 2014-7-13 1:37:43 org.apache.struts2.spring.StrutsSpr ...

  10. 北邮iptv用WindowsMediaplayer打不开的解决的方法

    前言:之前我的iptv能够用,可是有次我安装了realplayer,它就偷偷把iptv文件的默认打开方式给篡改了,卸载了                  realplayer之后,iptv不能直接用 ...