博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3579- Median
阅读量:7228 次
发布时间:2019-06-29

本文共 1457 字,大约阅读时间需要 4 分钟。



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

41 3 2 431 10 2

Sample Output

18

Source

, Lei Tao
题目大意:给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

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

你可能感兴趣的文章
MySQL日期函数
查看>>
【00】Effective Java
查看>>
.NET重构—单元测试重构
查看>>
SMB简介sabma服务(一)
查看>>
ANT简明教程
查看>>
Eclipse Luna WTP 与 Tomcat 8 的整合存在一个很头疼的 Bug
查看>>
小数在计算机里面的存放
查看>>
数据结构中的各种树简单解释
查看>>
我的朗科运维第七课
查看>>
CentOS的进程管理二
查看>>
https客户端证书导入
查看>>
用 PreparedStatement 向 SqlServer 中一次性插入多条记录
查看>>
Slackware-2014-0903
查看>>
CentOS下安装JDK1.7
查看>>
LDAP DIT设计参考
查看>>
iptables详解
查看>>
Protostuff 介绍
查看>>
一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别...
查看>>
参数验证其实可以更简明一点
查看>>
Set up Mule runtime env with mule-standalone-3.6.0
查看>>