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. python 学习笔记三 (函数)

    1.把函数视为对象 def factorial(n): '''return n!''' return 1 if n < 2 else n*factorial(n-1) print(factori ...

  2. Hive 教程(三)-DDL基础

    DDL,Hive Data Definition Language,数据定义语言: 通俗理解就是数据库与库表相关的操作,本文总结一下基本方法 hive 数据仓库配置 hive 数据仓库默认位置在 hd ...

  3. python中的并发执行

    一. Gevent实例 import gevent import requests from gevent import monkey # socket发送请求以后就会进入等待状态,gevent更改了 ...

  4. 滚动页面产生动画WOW.js的用法

    简介 在一些网页上,当你滚动页面的时候会看到各式各样的元素动画效果,非常动感.WOW.js 就是一款帮助你实现这种 CSS 动画效果的插件.WOW.js 依赖 animate.css,所以它支持 an ...

  5. 用data的方法获取值的时候,要注意的问题一定要在先封装好

    var art=$(".add-more").prev().find(".content").data("list1"); 我们一定要在aj ...

  6. Bash Plays with Functions CodeForces - 757E (积性函数dp)

    大意: 定义函数$f_r(n)$, $f_0(n)$为pq=n且gcd(p,q)=1的有序对(p,q)个数. $r \ge 1$时, $f_r(n)=\sum\limits_{uv=n}\frac{f ...

  7. 导入excel-uploadify+npoi

    总结: 导入文件时一定要给database设置栏位 虚拟表的栏位名称可以与数据库表不一致,但顺序一定要一致,因为它是按照顺序依次插入的 sheet.FirstRowNum = 0; sheet.Fir ...

  8. bash脚本测试总结

    bash脚本测试总结   跟踪脚本的执行 可以让bash打印出你脚本执行的过程中的所有语句.这很简单,只需要使用bash的-x选项就可以做到,下面让我们来看一下.   下面的这段脚本,先是输出一个问候 ...

  9. Tomcat 7 自动加载类及检测文件变动原理

    在一般的 web 应用开发里通常会使用开发工具(如 Eclipse.IntelJ )集成 tomcat ,这样可以将 web 工程项目直接发布到 tomcat 中,然后一键启动.经常遇到的一种情况是直 ...

  10. 第三篇 HTML 表单及表格

    表单及表格 表单,常用在登录.注册等地方,这也是一个最基本的.   我们就用登录,来学习什么是表单!   表单 form 标签,在某些好用的编辑工具,比如:WebStorm  你在上面写出form再按 ...