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. 纯前端表格控件SpreadJS V12.1 隆重登场,专注易用性,提升用户体验

    ​ 一款优秀的开发工具,在更新迭代中,除了要满足不同场景的业务需求,也需不断优化已有功能,尤其是细节方面,要能为用户带来使用体验和开发效率的提升. 作为一款备受业界专家和开发者认可的纯前端类Excel ...

  2. 分层最短路(牛客第四场)-- free

    题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...

  3. Jenkins的安装配置及使用

    一.以Jenkins在tomcat容器里运行的方式,jenkins的安装及安装时所涉及的JDK和tomcat的配置 1.首先下载tomcat, 2.下载Jenkins.war包,将war包放在tomc ...

  4. MySQL存储引擎MyISAM和InnoDB有哪些区别?

    一.MyISAM和InnoDB的区别有哪些? 1.InnoDB支持事务,MyISAM不支持.对于InnoDB每一条SQL语言都默认封装成事务,自动提交,这样会影响速度,所以最好把多条SQL语言放在be ...

  5. Seeker:一款可获取高精度地理和设备信息的工具分析

    Seeker是一款可以获取高精度地理和设备信息的工具.其利用HTML5,Javascript,JQuery和PHP来抓取设备信息,以及Geolocation接口实现对设备高精度地理位置的获取. See ...

  6. HashMap原理探究

    一.写随笔的原因:HashMap我们在平时都会用,一般面试题也都会问,借此篇文章分析下HashMap(基于JDK1.8)的源码. 二.具体的内容: 1.简介: HashMap在基于数组+链表来实现的, ...

  7. Linux下如何查看CPU型号、个数、核数、逻辑CPU数、位数、发行版本、内核信息、内存、服务器生产厂家

    [原文链接]:http://blog.csdn.net/mdx20072419/article/details/7767809 http://blog.chinaunix.net/uid-224252 ...

  8. php不重新编译,添加模块

    本文已安装mysqli模块为例 一:检查 1:首先保证php-fpm能正常启动 2:查看当前已安装的php模块是否有mysqli [root@oldboyedu ~]# /application/ph ...

  9. 02:Java基础语法(一)

    Java基础语法 Java的关键字及保留字 关键字(Keyword) 关键字的定义和特点定义:被Java语言赋予了特殊含义的单词特点:关键字中所有字母都为小写注意事项:1)true.false.nul ...

  10. python-嵌套函数

    python-嵌套函数 定义:在函数体内用def定义一个函数,它的作用域只在该函数体内有效. def outside(): print("int the outside") def ...