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

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

Source

题意:给你N个数字,求这些数字两两之差的绝对值的中位数
二分套二分:核心还是要抓住<x的数量>=m(内层二分)的最小x(外层二分)-1才是该序列中的第m小的数,有不理解的话可以看本博客另一篇讲的很详细的文章
下面的第一份代码是使用了lower_bound,复杂度是n(logn)*(logn);时间是563ms#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
int a[100005];
long long l,r,m,n;
int ok(int x)
{
int cnt=0;
for(int i=1;i<=n;i++)
cnt+=lower_bound(a+i,a+n+1,a[i]+x)-(a+i)-1;
//lower_bound降低时间复杂度
return cnt>=m;
}
int main()
{
while(~scanf("%lld",&n))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
m=n*(n-1)/2;
if(m%2==0) m=m/2;
else m=(m+1)/2;
l=0,r=a[n]-a[1];
while(r-l>1)
{
int mid=(r+l)>>1;///枚举得到<x的数量>=m的最小x;
if(ok(mid))
r=mid;
else
l=mid;
}
printf("%lld\n",r-1);
}
return 0;
}

  有个小技巧是在ok()函数内统计绝对值<x的数量可以不适用lower_bound而使用尺取法

可以降低复杂度,复杂度是(logn)*n,时间只有300多ms

int ok(int  x)
{
int cnt=0;
for(int i=1,j=1;i<=n;i++)
{
while(a[j]-a[i]<x&&j<=n)
j++;
cnt+=(j-1-i);
}
return cnt>=m;
}

  

poj 3579 Median 二分套二分 或 二分加尺取的更多相关文章

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

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

  2. POJ 3579 Median(二分答案)

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

  3. POJ 3579 Median 二分加判断

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

  4. POJ 3579 Median (二分)

                                                                                                         ...

  5. POJ 3579 Median 【二分答案】

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

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

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

  7. POJ 3579 median 二分搜索,中位数 难度:3

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

  8. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

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

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

随机推荐

  1. 小菜鸟之Oracle数据库

    select * from STUDENT; select * from mark; select * from COURSE; select * from teacher; --注释 select ...

  2. java this的作用

    this: 区分成员变量和局部变量的重名问题,this.name指的是类中的成员变量,而不是方法内部的

  3. ARM Cortex-M 系列 MCU 错误追踪库 心得

    一. 感谢CmBacktrace开源项目,git项目网站:https://github.com/armink/CmBacktrace 二. 移植CmBacktrace 2.1 准备好CmBacktra ...

  4. Fidder抓包设置

    1, 谷歌浏览器中:

  5. Hive 教程(五)-参数配置

    配置基本操作 hive> set; 查看所有配置hive> set key: 查看某个配置hive> set key value: 设置某个配置 我们可以看到一些 hadoop 的配 ...

  6. Ruby初见

    一. 简介 Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License. 二 ...

  7. Linux-2.1vim简单使用

    1.用vim打开文件,文件内容显示在终端,命令模式无法编辑 vim 1.txt vi 1.txt 2.使用a,i,o,shift+o编辑文件,按Esc退出编辑模式 i 插入在光标前 a 插入在光标后 ...

  8. Ansible 常用模块详解

    经过前面的介绍,我们已经熟悉了 Ansible 的一些常识性的东西和如何编译安装Ansible,从本章开始我们将全面介绍 Ansible 的各种生产常用模块,这些也是我们使用 Ansible 的过程中 ...

  9. Peter and Snow Blower CodeForces - 613A (点到线段距离)

    大意: 给定多边形, 给定点$P$, 求一个以$P$为圆心的最小的圆环包含整个多边形. #include <iostream> #include <cmath> #define ...

  10. MySQL性能优化(六):分区

    原文:MySQL性能优化(六):分区 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbi ...