POJ 3579- Median
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
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的更多相关文章
- poj 3579 Median (二分搜索之查找第k大的值)
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- POJ 3579 Median(二分答案+Two pointers)
[题目链接] http://poj.org/problem?id=3579 [题目大意] 给出一个数列,求两两差值绝对值的中位数. [题解] 因为如果直接计算中位数的话,数量过于庞大,难以有效计算, ...
- POJ 3579 Median(二分答案)
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...
- POJ 3579 Median 二分加判断
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12453 Accepted: 4357 Descripti ...
- POJ 3579 Median (二分)
...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- POJ 3579 median 二分搜索,中位数 难度:3
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3866 Accepted: 1130 Descriptio ...
- POJ 3579 Median 【二分答案】
<题目链接> 题目大意: 给出 N个数,对于存有每两个数的差值的序列求中位数,如果这个序列长度为偶数个元素,就取中间偏小的作为中位数. 解题分析: 由于本题n达到了1e5,所以将这些数之间 ...
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
- 【POJ - 3579 】Median(二分)
Median Descriptions 给N数字, X1, X2, ... , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤N). 我们能得到 C(N,2) 个 ...
随机推荐
- PCB板蛇形走线有什么作用
PCB板蛇形走线有什么作用 PCB上的不论什么一条走线在通过高频信号的情况下都会对该信号造成时延时,蛇形走线的主要作用是补偿"同一组相关"信号线中延时较小的部分,这些部分一般是没 ...
- Visual Studio Tips: How to change project namespace
/* Author: Jiangong SUN */ If you want to modify a project's namespace and its physical container na ...
- 小米2S Mk6.0.1 [只能做测试体验,不能使用]
上几张高清图片.. 说明: 此版本只能做测试体验,不能做实际使用. 开发者: laser杨万荣 感谢: 秋叶随风ivan, m1cha 及 MoKee Open Source的各位开发者 下载地址:链 ...
- 新书《iOS8 Swift编程指南》货架
颐和园的新书出版. 链接:http://www.amazon.cn/dp/B00YOQSYAO 这本书从去年开始7可能开始写.今年1完成这个月的第一稿,经过多次修改,今天,最后的正式出版,欢迎大家指正 ...
- JDK源码学习系列03----StringBuffer+StringBuilder
JDK源码学习系列03----StringBuffer+StringBuilder 由于前面学习了StringBuffer和StringBuilder的父类A ...
- 【转】Android Http Server
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://vaero.blog.51cto.com/4350852/939413 Andro ...
- CallContext和多线程
前一段时间正好要在某个网页程序上开一个多线程调用多个组件的尝试,这些组件是有其他团队开发的(如:印度/俄罗斯),所以修改它们的代码看起来是不太现实的,但是,令人恼火的是他们的代码中大量的用到了AppC ...
- linux挂载U盘,及乱码问题解决
1. 首先使用切换到root用户. 2. 使用fdisk -l命令查看磁盘信息,找到u盘(能够依据显示的大小确定) 3. 在/mnt下创建挂载点,比如创建usb目录:mkdir /mnt/usb 4. ...
- [MSSQL]最小公约数
[摘要]一个朋友在展BOM的时候有这种需求,两列字段(数值):A ,B A=用量,B=底数,组成用量=用量/底数.A/B,若能被整除,显示整除的结果,若不能整除显示分数形式A/B(分数形式要是约分 ...
- STM32本学习笔记EXTI(外部中断)
参考资料:STM32数据表.网络信息 =========================================切割线===================================== ...