Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3528   Accepted: 1001

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

 
二分中位数
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define maxn 100005
#define INF 200005
typedef long long ll; int n;
int a[maxn],dis[maxn];
ll num; bool judge(int x) { int pos,i = ,now = ;
ll sum = ;
while(i < n) {
pos = upper_bound(dis + ,dis + n + ,x + now) - dis;
sum += n - (pos);
now = dis[i];
if((n - ) - pos + == ) break;
++i;
} //printf("x = %d sum = %lld\n",x,sum); return num - sum - (num % ) >= sum;
} void solve() { num = (ll)n * (n - ) / ; int l = INF,r = a[n] - a[];
for(int i = ; i < n; ++i) {
dis[i] = a[i + ] - a[];
l = min(l,a[i + ] - a[i]);
// printf("%d ",dis[i]);
}
dis[n] = INF; //printf("l = %d r = %d\n",l,r); while(l < r) {
int mid = (l + r) >> ;
if(judge(mid)) {
r = mid;
} else {
l = mid + ;
}
} printf("%d\n",l); } int main()
{
// freopen("sw.in","r",stdin); while(~scanf("%d",&n)) {
for(int i = ; i <= n; ++i) {
scanf("%d",&a[i]);
} sort(a + ,a + n + ); solve();
} return ;
}

POJ 3579的更多相关文章

  1. POJ 3579 3685(二分-查找第k大的值)

    POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...

  2. POJ 3579 Median(二分答案+Two pointers)

    [题目链接] http://poj.org/problem?id=3579 [题目大意] 给出一个数列,求两两差值绝对值的中位数. [题解] 因为如果直接计算中位数的话,数量过于庞大,难以有效计算, ...

  3. poj 3579 Median (二分搜索之查找第k大的值)

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

  4. POJ 3579 Median(二分答案)

    Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...

  5. POJ 3579 Median 二分加判断

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12453   Accepted: 4357 Descripti ...

  6. POJ 3579 二分

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7687   Accepted: 2637 Descriptio ...

  7. POJ 3579 Median (二分)

                                                                                                         ...

  8. poj 3579 Median 二分套二分 或 二分加尺取

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5118   Accepted: 1641 Descriptio ...

  9. Divide and conquer:Median(POJ 3579)

        快速求两数距离的中值 题目大意:给你一个很大的数组,要你求两个数之间的距离的中值 二分法常规题,一个pos位就搞定的事情 #include <iostream> #include ...

随机推荐

  1. Java web的读取Excel简单Demo

    目录结构: Data.xls数据:   后台页面: GetExcelData.java       public void doGet(HttpServletRequest request, Http ...

  2. input中的id和name

    name在以下用途是不可替代的: 1. 表单(form)的控件名,提交的数据都用控件的name而不是id来控制.因为有许多name会同时对应多个控件,比如checkbox和radio,而id必须是全文 ...

  3. nginx安装总结

    对于nginx作为负载均衡服务器时的安装需要安装rewrite模块需要的pcre()库,gzip模块需要zlib库,ssl模块需要openssl库,对此依赖安装有很多种处理方式,以下简单总结: 通过源 ...

  4. Android API Level与sdk版本对照表

    API等级1: Android 1.0 API等级2: Android 1.1 Petit Four 花式小蛋糕 API等级3: Android 1.5 Cupcake 纸杯蛋糕 API等级4: An ...

  5. (转)可收缩、扩展的TextView

    在一些应用中,比如腾讯的应用市场APP应用宝,关于某款应用的介绍文字,如果介绍文字过长,那么不是全部展现出来,而是显示三四行的开始部分(摘要),预知全部的内容,用户点击展开按钮即可查阅全部内容.这样的 ...

  6. [转]init.d解析

    本文为转载,放在这里以备忘. init.d指的是/etc/rc.d/init.d目录.本文包括3部分内容1. Linux的引导过程2. 运行级别3. /etc/rc.d/ 与/etc/rc.d/ini ...

  7. python autopy

    今天学习了python autopy 主要包括alert,color,mouse,key,bitmap,screen等的操作 文档在http://www.autopy.org/documentatio ...

  8. IE6和IE7的line-height和现代浏览器不一致的问题

    1.我们发现在网页中设置line-height后,现代浏览器显示正常,可是在IE6 IE7下却不能正确解析,这时需要再额外的为旧版浏览器声明: p{ line-height: 30px; *line- ...

  9. 括弧匹配检验(check)

    /*题目:括弧匹配检验 检验给定表达式中括弧是否正确匹配 (两种括弧“( ) ”“[]" ,正确输出OK,错误则输出wrong. 2016年8月8日07:24:58 作者:冰樱梦 */ # ...

  10. Keil uVision4 代码编辑器中文字符乱码问题

    MDK-ARM 使用中一直有个很纠结的问题,中文字符支持不好. 比如写代码注释,使用中文删除字符就会只删除一半问题.复制粘贴代码中间有中文就会出现乱码问题. 想过换IAR,新学个IDE也麻烦,上面的问 ...