Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3866   Accepted: 1130

Description

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
思路: 一眼可知把原数列a排列一遍的时间还是有的,但是要想得到两两之差然后归并则T,M都超,计数范围也太大了,
把a排序之后两两相减,得到相邻差数组b,那么b就可以构成所有需要的差了,
那么现在假设有个差delta,使得a[i]+delta<=a[j],a[i]+delta>a[j-1],那么delta一定大于a[j-1]-a[i],a[j-2]-a[i]....delta一定小于等于a[j]-a[i],a[j+1]-a[i],
使用low_bound可以在logn时间内求出j,nlogn时间就可以求出所有比delta大的差的个数,
这时就可以判断是否delta算是差的中位数.
那么直接二分差即可,总时间n*logn*logn,5*1e7-1e8的数量级,可以跑过
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
long long halfh,h;
int n;
bool judge(int mid){
long long cnt=0;
for(int i=0;i<n;i++){
cnt+=a+n-lower_bound(a+i,a+n,a[i]+mid);
}
return cnt>halfh;
}
int main(){
while(scanf("%d",&n)==1){
h=n*(n-1)/2;halfh=h/2;
for(int i=0;i<n;i++){scanf("%d",a+i);}
sort(a,a+ n);
int l=0,r=a[n-1],mid;
while(r-l>1){
mid=r+l>>1;
if(judge(mid)){
l=mid;
}
else r=mid;
}
printf("%d\n",l);
}
}

  

 

POJ 3579 median 二分搜索,中位数 难度:3的更多相关文章

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

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

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

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

  3. POJ 3579 Median(二分答案)

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

  4. POJ 3579 Median 二分加判断

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

  5. POJ 3579 Median (二分)

                                                                                                         ...

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

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

  7. POJ 3579 Median 【二分答案】

    <题目链接> 题目大意: 给出 N个数,对于存有每两个数的差值的序列求中位数,如果这个序列长度为偶数个元素,就取中间偏小的作为中位数. 解题分析: 由于本题n达到了1e5,所以将这些数之间 ...

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

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

  9. POJ 3579:Median 差值的中位数

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4680   Accepted: 1452 Descriptio ...

随机推荐

  1. MySQL timespan设置 sql_mode设置

    Timespan设置: 在MySQL5.7版本中创建表 CREATE TABLE `investor_seat` ( `id` int(11) NOT NULL AUTO_INCREMENT , `i ...

  2. HDU 1848 Fibonacci again and again(SG函数入门)题解

    思路:SG打表 参考:SG函数和SG定理[详解] 代码: #include<queue> #include<cstring> #include<set> #incl ...

  3. ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer 最大生成树 lca

    大概就是要每两个点 只能有一条路径,并且约束,最短的边用来砌墙,那么反之的意思就是最大的边用来穿过 故最大生成树 生成以后 再用lca计算树上两点间的距离 (当然防止生成树是一条链,可以用树的重心作为 ...

  4. Educational Codeforces Round 53 Editorial

    After I read the solution to the problem, I found that my solution was simply unsightly. Solved 4 ou ...

  5. 读jQuery源码有感

    读之前的预备工作: 1.基础的js知识,以及html和css知识,和正则表达式知识.可以参考妙味课堂的基础js,html和css大纲. 2.JavaScript核心指南的知识http://www.cn ...

  6. UOJ #79. 一般图最大匹配

    板子: #include<iostream> #include<cstdio> #include<algorithm> #include<vector> ...

  7. python 时间元组转可视化时间

    >>> import time >>> time.asctime() 'Fri Jan 4 11:17:20 2019' >>> time.asc ...

  8. 解决Resource doesn't have a corresponding Go package.问题

    首先上图 这个报错主要是程序要启动没有入口的原因,package main下边的mian方法才是一个程序的入口.这就要 修改目录结构如下图修改并运行就可以了

  9. JavaScript权威指南--多媒体和图形编程

    知识要点 21.1节介绍如何用传统的JavaScript技术实现诸如图片翻转(鼠标指针移动到一张静态图片上切换成另外一张图片)这样的视觉效果. 21.2节介绍HTML5的<audio>和& ...

  10. Java 常用对象-Date类和Calender类

    2017-11-02 22:29:34 Date类:类 Date 表示特定的瞬间,精确到毫秒. 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值 ...