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. IIS7多站点ssl配置及http自动跳转到https

    SSL证书配置参考如下: http转https实战教程iis7.5 window08 IIS7安装多域名SSL证书绑定443端口 关键是修改C:\Windows\System32\inetsrv\co ...

  2. servlet_cdi自动注入

    @WebServlet("/cdiservlet")//url映射,即@WebServlet告诉容器,如果请求的URL是"/cdiservlet",则由NewS ...

  3. mybatis整体流程

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

  4. Luogu P1600[NOIP2016]day1 T2天天爱跑步

    号称是noip2016最恶心的题 基本上用了一天来搞明白+给sy讲明白(可能还没讲明白 具体思路是真的不想写了(快吐了 如果要看,参见洛谷P1600 天天爱跑步--题解 虽然这样不好但我真的不想写了 ...

  5. liunx 安装rsync

    新建一个rsync.s文件,把下面的代码写入文件里: #!/usr/bin/env bash mkdir -p /data/app/rsync/etc/ mkdir -p /data/logs/rsy ...

  6. 测试必知150个常用Linux命令,已为各位筛选整理

    ●线上查询及帮助命令(1 个) help   如:mkdir --help   ●文件和目录操作命令(12 个)    ls tree pwd mkdir rmdir cd touch cp mv r ...

  7. 2019最新的IDEA的激活方式!!!

    第一步: 安装IDEAhttps://www.jetbrains.com/idea/ 选择要下载的版本Ultimate 第二步: 下载破解补丁链接:https://pan.baidu.com/s/1j ...

  8. 第一篇 jQuery

    1-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  9. O005、远程管理 KVM 虚机

    参考https://www.cnblogs.com/CloudMan6/p/5256018.html   上一节我们通过 virt-manager 在本地主机上创建并管理 KVM 虚机,其实 virt ...

  10. ab测试工具的使用

    下载地址:http://httpd.apache.org/download.cgi#apache24 编译安装后在安装目录bin下可以找到ab执行程序 基本用法: ab -n 5000 -c 1000 ...