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. webjars使用

    目的: Web前端使用了越来越多的JS或CSS,如jQuery,Backbone.js和Bootstrap.一般情况下,我们是将这些Web资源拷贝到Java Web项目的webapp相应目录下进行管理 ...

  2. mongodb副本集仲裁节点搭建

    服务器准备: 主节点192.168.100.106 从节点192.168.100.107 仲裁节点192.168.100.108 三台服务器: 关闭防火墙 service iptables stop ...

  3. WORKDIR 指定工作目录 每一个 RUN 都是启动一个容器、执行命令、然后提交存储层文件变更

    WORKDIR 指定工作目录 格式为 WORKDIR <工作目录路径>. 使用 WORKDIR 指令可以来指定工作目录(或者称为当前目录),以后各层的当前目录就被改为指定的目录,如该目录不 ...

  4. matplotlib学习(1)

    1.基本学习(1)1.1 代码: import matplotlib.pyplot as plt import numpy as np x=np.linspace(-1,1,50) #从-1到1,共5 ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:响应式的列重置

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 基于Modelsim的视频捕获模拟仿真

    一.前言 针对牟新刚编著的<基于FPGA的数字图像处理原理及应用>中第五章系统仿真中关于视频捕获模拟的例子进行补充和仿真验证,简言之,吊书袋子. 2020-02-27 21:09:05 二 ...

  7. 科幻电影免费百度云分享(Scince-fiction cloud share)

    Marvel episode Link  Passcode:6h9k Star War full episode Link Passcode:7abk Men In Black Episode Col ...

  8. MySQL忘记密码如何重置

    一]进入服务器下,我用的是centos版本 vim /etc/my.cnf 1 vim[二]找到mysqld的部分然后在下面添加上一句代码,意思是跳过密码直接进入,然后保存退出 skip-grant- ...

  9. SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible

    SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible ...

  10. Newtonsoft.Json 版本不一致导致错误

    可以在配置文件添加这部分,其他版本的不一致,也可使用这种方式解决. <runtime> <assemblyBinding xmlns="urn:schemas-micros ...