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

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个数,然后求它们两两相减的绝对值,然后找出这些绝对值的中位数

【分析】

1、先对n个数排序,那么最后的结果ans一定满足0<=ans<an-a1

2、我们用二分进行逼近。l=0,r=an-a1,mid=(l+r)/2;

3、很明显是原数组两个数相减的绝对值<mid个数,和>mid的个数进行比较。(绝对值的个数<mid,ans在[mid,r]区间,否则在[l,mid]区间内)

4、以求一个数减去a[1]的值小于mid的个数为例,我们找到一个a[i]>=a[1]+mid时最小的一个i,那么数组[1,i)值减去a[1],都小于mid,这样枚举i就可以求得两数相减差值(的绝对值)小于mid的个数。

【代码】

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
inline int read(){
register char ch=getchar();register int x=0;
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x;
}
const int N=2e5+5;
int n,a[N];ll m;
inline bool check(int now){
ll sum=0;
for(int i=1;i<=n;i++)
sum+=lower_bound(a+i+1,a+n+1,a[i]+now)-(a+i)-1;
return m&1?sum<=m/2:sum<m/2;
}
int main(){
while(scanf("%d",&n)==1){
for(int i=1;i<=n;i++) a[i]=read();
sort(a+1,a+n+1);m=1LL*n*(n-1)>>1;
int l=0,r=a[n]-a[1],mid,ans=0;
while(l<=r){
mid=l+r>>1;
if(check(mid)){
ans=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
printf("%d\n",ans);
}
return 0;
}

POJ 3579 Median(二分答案)的更多相关文章

  1. POJ 3579 Median 二分加判断

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

  2. POJ 3579 Median (二分)

                                                                                                         ...

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

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

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

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

  5. POJ 3579 Median 【二分答案】

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

  6. POJ 3104 Drying(二分答案)

    题目链接:http://poj.org/problem?id=3104                                                                  ...

  7. POJ 3122 Pie 二分答案

    题意:给你n个派,每个派都是高为一的圆柱体,把它等分成f份,每份的最大体积是多少. 思路: 明显的二分答案题-- 注意π的取值- 3.14159265359 这样才能AC,,, //By Sirius ...

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

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

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

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

随机推荐

  1. Node.js安装和入门 - 2行代码让你能够启动一个Server

    转自:http://josh-persistence.iteye.com/blog/1979552  备忘 Node.js是一个轻松构建快速,可扩展的网络应用平台建立在Chrome的JavaScrip ...

  2. SharePoint PowerShell部署开发好的WebPart到服务器上

    内容仅供参考,需结合实际需求来处理. =========SharePoint 环境下运行ps1文件,ps1内容如下======= Set-ExecutionPolicy ByPass Add-PSSn ...

  3. 非抢占式RCU实现(二),解释:为什么 RCU_NEXT_SIZE 宏值是4?

    参考:2.6.34 一个很奇怪的问题. 没有查找到为什么 RCU_NEXT_SIZE的值为4的原因(包括Documentation),主要是在rcu_state中定义了一个四级的list,感到很有意思 ...

  4. difference between TotalFreeSpace and AvailableFreeSpace

    Refer:http://stackoverflow.com/questions/7275806/what-is-the-difference-between-totalfreespace-and-a ...

  5. linux环境中,如何查看某个软件包,都依赖哪些软件包?被哪些软件包依赖?

    需求描述: 今天查看主机上的一个软件ghostscript,准备删除,就先看下这个软件都依赖哪些软件, 然后呢被哪些软件依赖 操作过程: 1.查看这个软件依赖哪些软件(rpm方式查询) [root@t ...

  6. Python 和C#的交互

    IronPython是一个.NET平台上的Python实现,包括了完整的编译器.执行引擎与运行时支持,能够与.NET已有的库无缝整合到一起. IronPython已经很好的集成到了.NET frame ...

  7. HTML5标签canvas制作平面图

    摘要: HTML5规范已经完成了,互联网上已经有数不清的站点使用了HTML5.从现在开始研究HTML5,本文是自己在学习canvas过程中的记录,以备后需. 历史: 这个 HTML 元素是为了客户端矢 ...

  8. [RN] 03 - Resource Collection & AWS Auth

    那些资源 一.三个例子 iReading Bilibili-React-Native ZhiHuDaily-React-Native 二.IM 例子 1. React Native Socket.io ...

  9. Windows 7 无密码文件共享

    Windows7中创建无密码的文件共享的几个步骤: 在“控制面板\所有控制面板项\网络和共享中心\高级共享设置”开启“关闭密码保护共享”和“启用文件和打印机共享”.关闭密码保护共享的操作会启用Gues ...

  10. error C4996: Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct

    使用VS13 跟  google protocbuf时出现了这个问题:真蛋疼,用别人的东西你就说不安全,用你自己的东西时你怎么不说不安全来着! 解决方案 在protoc   生成的头文件中加上 #pr ...