Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4680   Accepted: 1452

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

题意是给出一个数组,然后这些数组元素每一对之间都有一个差值,找出这些差值的中位数。

两次二分,第一次二分是枚举答案,第二次二分是我觉得很好玩的地方,在给定的差值下,找到多少个对的差值小于等于它,用这样的二分来判断这个差值在所有的差值中的位置。感觉很巧妙~

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int num,m;
int val[100005]; bool check(int mid)
{
int i,sum=0; for (i = 0; i < num; i++)
{
sum += (lower_bound(val + i, val + num, val[i] + mid + 1) - (val + i) - 1);
}
if (sum >= m)
{
return true;
}
else
{
return false;
}
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,left,right,mid;
while (scanf("%d", &num) != EOF)
{
for (i = 0; i < num; i++)
{
scanf("%d", &val[i]);
}
m = (num*(num - 1) / 2 + 1) / 2;
sort(val,val+num); left = 0;
right = val[num - 1] - val[0]; while (right-left > 1)
{
mid = (left + right) / 2; if (check(mid))
{
right = mid ;
}
else
{
left = mid ;
}
}
cout << right << endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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 (二分)

                                                                                                         ...

  4. POJ 3579 Median 【二分答案】

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

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

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

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

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

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

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

  8. POJ 3579 Median 二分加判断

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

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

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

随机推荐

  1. Nginx安装部署!

    安装Nginx方法一:利用u盘导入Nginx软件包 二nginx -t 用于检测配置文件语法 如下报错1:配置文件43行出现错误 [root@www ~]# nginx -tnginx: [emerg ...

  2. SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible

    SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible ...

  3. 【摘录自MDN】对事件冒泡和捕捉的解释

    当一个事件触发了一个有父元素的元素(例如我们的<video>时),现代浏览器运行两个不同的阶段 - 捕获阶段和冒泡阶段. 在捕获阶段: 浏览器检查元素的最外层祖先(<html> ...

  4. springmvc_ajax异步上传文件(基于ajaxfileupload.js)

    引入js <script th:src="@{/js/ajaxfileupload.js}"></script> html <tr> <t ...

  5. 4.ORM框架的查询

    创建表对应关系代码如下: from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy app=Fl ...

  6. bootloader与启动地址偏移

    如果项目工程是IAP+APP,则在keil的APP中要么在修改IROM/IRAM的开始地址和大小,并在MAP中勾选设置. 在NVIC中修改system_stm32f10x.c修改 这个在void Sy ...

  7. nodeJS - 定义全局变量

    定义 : global.变量名=‘xxxx’; 取出 :    global.变量名

  8. 5.Nginx

    1.Nginx 安装 (1) 安装gcc (yum install gcc) 备注:可以输入gcc -v 查询版本信息,看系统是否自带安装 (2) 安装pcre (yum install pcre-d ...

  9. 查漏补缺之Go的Strings, bytes, runes和字符

    字节遍历,字符遍历 https://play.golang.org/p/DeZcCN9aHXo package main import ( "fmt" "unicode/ ...

  10. 网络请求中的URL中传bool型数据

    如果在URL中要拼接bool的数据,OC这边不能使用BOOL型.因为使用NSString的拼接字符串类方法中,会将BOOL型数据转化为0或者1. 解决办法: NSString *overdue_str ...