Median

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10335   Accepted: 3630

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 = 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

POJ Founder Monthly Contest – 2008.04.13, Lei Tao

问题链接:POJ3579 Median

问题描述

n个数Xi(1<=i<=n),求其两两差的最中间的值。

问题分析

使用二分法实现,并且使用STL的算法函数lower_bound()。

程序说明

算法函数lower_bound()的功能是,查找有序区间中第一个大于或等于某给定值的元素的位置。

参考链接:(略)

题记:(略)

AC的C++语言程序如下:

/* POJ3579 Median */#include <iostream>
#include <algorithm>
#include <stdio.h>using namespace std;const int N = 1e5;
int n, m, a[N];bool judge(int v)
{long long cnt = 0;for (int i = 0; i < n; i++)cnt += a + n - lower_bound(a + i, a + n, a[i] + v);return cnt > m;
}int main()
{while(~scanf("%d", &n)) {for(int i = 0; i < n; i++)scanf("%d", &a[i]);sort(a, a + n);m = n * (n - 1) / 4;int lb = 0, ub = a[n-1], mid = (lb + ub) / 2;while (ub - lb > 1) {if (judge(mid))lb = mid;elseub = mid;mid = (lb + ub) / 2;}printf("%d\n", mid);}return 0;
}

POJ3579 Median【二分法+中位数】相关推荐

  1. c++median search中位数搜索的实现算法(附完整源码)

    C++median search中位数搜索的实现算法 C++median search中位数搜索的实现算法完整源码(定义,实现,main函数测试) C++median search中位数搜索的实现算法 ...

  2. HDU 3282 Running Median 动态中位数,可惜数据范围太小

    Running Median Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  3. poj3784 Running Median查找中位数

    参考http://blog.csdn.net/sdfzyhx/article/details/52735387 凡是关于排序,可以考虑优先队列. #include<cstdio> #inc ...

  4. POJ3784-Running Median(运行中位数)【链表】

    正题 题目链接:http://poj.org/problem?id=3784 题目大意 给出n个数,每两个数输出一次到目前为止输入了的所有数的中位数. 解题思路 我们使用离线算法. 既然这是一个一个输 ...

  5. 【POJ3784】Running Median(中位数,对顶堆)

    description 输入M个数,当已输入的个数为奇数个时输出此时的中位数. 一共有M/2+1个中位数要输出,每一行10个. solution 维护两个优先队列. 大根堆q1维护比当前中位数小的元素 ...

  6. [二分法]leetcode4:寻找两个有序数组的中位数(hard)

    题目: 题解: 又是一道hard题啊,这对于原来就很少做hard的我来说,可以说是很锻炼人了,坐下来静心思考,拿纸笔画结果,写推导过程,终于把这道题搞定了. 思路: 二分法,中位数就是将数组划分为长度 ...

  7. O‘REILLY: Median —— 中位数

    CheckIO是一个通过闯关游戏学习编程的网站(Python和JavaScript).通过解题开发新"岛屿",同时,通过做任务获得Quest Points解锁会员题目. 文章内容: ...

  8. 聚合函数 -AVG/MAX/MIN/STDDEV/VARIANCE/SUM/COUNT/MEDIAN

    ------------------------------------------聚合函数-----------------------------------------------1: AVG( ...

  9. 获取中位数java_java 计算中位数方法

    最近工作需要 要求把python的代码写成java版本,python中有一个np.median()求中位数的方法,java决定手写一个 先说说什么是中位数: 中位数就是中间的那个数, 如果一个集合是奇 ...

最新文章

  1. 如何让SQLServer的 itemNum 字段 按照数字大小顺序排序
  2. 简述python中怎样导入模块_12 python中模块和包如何导入
  3. 移动端安全 - 安卓Android - 漏洞修复方案整理
  4. 便把他手中的i698源代码拐杖转了一圈
  5. java listnode 合并链表_剑指offer:合并两个排序的链表(Java)
  6. Django模板层:内置模板标签if,for in(forloop.counter)with,url,spaceless,autoescape,verbatim,firstof,block,cycle
  7. Zookeeper Python
  8. 聊聊 API Gateway 和 Netflix Zuul
  9. linux关闭邮件提示错误,LINUX命令关闭 You have mail in /var/spool/mail/root邮件提醒功能...
  10. del/xcopy时如何在输出窗口中,不显示文件名
  11. 张口就问“CCIE”月薪多少
  12. NDK-JNI实战教程(二) JNI官方中文资料
  13. eyoucms添加附属栏目功能
  14. 2017人人都是产品经理年度作家评选结果公告
  15. 1ke android逆向助手_android逆向助手
  16. Java面试常考之 单例设计模式(饿汉式单例、 懒汉式单例)
  17. 怎么配置android远程服务器地址,在 Android studio 中 配置Gradle 进行 “动态编译期间,指定 远程服务器地址 ,生成多个安装包”...
  18. Meta 用算法“随机”解雇 60 人;印度的“网络霸凌”最严重;越南要求外国公司数据存储本地化| 每日大事件...
  19. 普通磨削、超声辅助磨削表面形貌代码及表面轮廓提取
  20. 软件工程之软件过程模型

热门文章

  1. MySQL数据类型特征_Mysql学习(三)数据类型_mysql
  2. pymysql之常见数据库操作
  3. 来自reallh大的游戏编程饕餮大餐!
  4. python怎么读excelsheet_python怎么读写excel文件
  5. 部署SpringBoot项目到腾讯云或其他服务器
  6. spark application 同时运行 实行数据共享
  7. php 微信转发朋友圈,php微信分享到朋友圈、QQ、朋友、微博
  8. 扩展-视图 View
  9. electronjs设置宽度_Js操作DOM元素及获取浏览器高宽的简单方法
  10. tensorflow精进之路(二十八)——人脸识别(下)(MTCNN人脸检查和人脸对齐+CASIA-WebFace数据集模型)