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) 个 ...
随机推荐
- 三星galaxy S4快捷功能
你不知道的s4那些快捷操作全面挖掘 1.截屏:S4有三种截屏方法: 一种是常见的同一时候按住home键和电源键大概2秒左右时间. 另外一种是打开手势感应,设定→我的设备→动作与手势→手掌动作→截取屏幕 ...
- sdut 6-2 多态性与虚函数
6-2 多态性与虚函数 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1000ms Memory Limit ...
- poj1236 有向图加边变成强连通图
给我们一个有向图,有两个问题 1.最少要给多少个点发消息,才能使得所有的点都收到消息(消息可以随边传递) 2.最少需要多少条边才能使得图变成强连通图 对于一个强连通分量,可以当做一个点来考虑,所以我们 ...
- C#遍历FTP文件夹/下载
原文链接:http://blog.csdn.net/ou8811/article/details/5295780 整个程序大致可以分为2个部分,第一部分是实现单个文件下载的方法 [c-sharp] v ...
- ExcelHelper Excel,Export,Import
using System; using System.Collections.Generic; using System.Data; using System.Data.Odbc; using Sys ...
- Effective C++规定45 额外的代码
这部分是额外的代码博客,关键45术语思想已经实现. #include<iostream> using namespace std; template<typename T> c ...
- Form表单中的action路径问题
今天刚接触web,在用jsp和servlet做一个简单的登陆的时候在Form表单action属性和method属性的一些问题: 我遇到的是Form表单提交到servelet处理时遇到的问题: (1 ...
- Codeforces 107B Basketball Team 简单概率
题目链接:点击打开链接 题意: 给定n m h 表示有m个部门,有个人如今在部门h 以下m个数字表示每一个部门的人数.(包含他自己) 在这些人中随机挑选n个人,问挑出的人中存在和这个人同部门的概率是多 ...
- POJ1469_COURSES(二部图最大匹配)
解决报告 http://blog.csdn.net/juncoder/article/details/38136065 题目传送门 题意: n个学生p门课程,每一个学生学习0或1以上的课程. 问:能否 ...
- hibernate 基本和简单易用
首先hibernate.cfg.xml构造,在该文件src文件夹 <!DOCTYPE hibernate-configuration PUBLIC "-// ...