Given N numbers, X1X2, ... , 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 = 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 X1X2, ... , XN, ( X≤ 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 思路:直接N^2肯定行不通,那么我们可以二分差值,满足单调且可验证,最小最大问题,代码如下:
const int maxm = ;

int n, buf[maxm], m;

bool check(int d) {
int sum = ;
for (int i = ; i < n; ++i) {
sum += upper_bound(buf + i, buf + n, buf[i] + d) - buf - i - ;
}
return sum >= m;
} int main() {
while(scanf("%d",&n) != EOF) {
for (int i = ; i < n; ++i)
scanf("%d", &buf[i]);
sort(buf, buf + n);
m = n * (n - ) / ;
if(m % == )
m = m / ;
else
m = m / + ;
int l = , r = buf[n - ] - buf[], mid;
while(l <= r) {
mid = (l + r) >> ;
if(check(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
return ;
}
												

Day3-O-Median POJ3579的更多相关文章

  1. POJ3579 Median

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

  2. POJ3579 Median —— 二分

    题目链接:http://poj.org/problem?id=3579 Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

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

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

  4. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  5. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  6. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. Applying vector median filter on RGB image based on matlab

    前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...

  8. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  9. Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing

    B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...

  10. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

随机推荐

  1. vue 事件总线(bus)

    1.全局引入bus Vue.prototype.$bus = new.Vue() 2.组件间传值使用(在发送事件时接收组件会实时接收到, 可以用做兄弟组件间相互传值, 但页面跳转组件间有问题 通过$e ...

  2. Java 11 New Features

    前言 北京时间 2018年9 月 26 日,Oracle 官方宣布 Java 11 正式发布.这是 Java 大版本周期变化后的第一个长期支持版本,非常值得关注.从官网即可下载, 最新发布的 Java ...

  3. python的matplotlib的热门可视化动图

    1.图 2.代码 import pandas as pd import matplotlib as mpl import matplotlib.pyplot as plt import matplot ...

  4. jquery 分页 Ajax异步

    //使用Ajax异步查询数据 <div class="table-responsive"> <table class="table table-bord ...

  5. iOS APP语言国际化之应用内切换语言环境

    最近接了一个项目,需求是要做一款应用的英文版本,客户并不清楚,以为要另做一个APP.沟通后告诉他们在之前应用基础上加个国际化功能就好,把之前的语言国际化重新梳理记录一下. 一般设置更改本地语言环境后, ...

  6. ES查询不重复的数据

    GET ana-apk/_search #查询不重复的mac地址{  "size": 10,  "aggs": {    "MAC": {  ...

  7. centos7使用docker制作tomcat本地镜像

    1.安装Docker 安装docker前请确认当前linux的内核版必须是3.10及以上 命令: uname  -r 1).yum install -y yum-utils device-mapper ...

  8. github 初体验

    一.什么是 Github? github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开. Github 由Chris Wanstrath, ...

  9. maven设置jdk版本

    方法一:在maven文件夹下的settings.xml中添加 <profile> <id>jdk-1.8</id> <activation> <a ...

  10. 记数问题(0)<P2013_1>

    记数问题 (count.cpp/c/pas) [问题描述]  试计算在区间1到n的所有整数中,数字x(0≤x≤9)共出现了多少次?例如,在1到11中,即在1.2.3.4.5.6.7.8.9.10.11 ...