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

Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8286   Accepted: 2892

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

 
 
 
 
题解:
1.对数组进行排序。计算出有多少对数,并计算出中位数所在的位置m。
2.二分中位数mid,然后检测有多少对数的差小于等于mid。假设有cnt对,如果cnt>=m,那么缩小中位数;否则扩大中位数。
3.注意:upper_bound()、lower_bound()的区间是前闭后开
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; int n, a[MAXN];
int m; bool test(int mid)
{
int cnt = ;
for(int i = ; i<=n; i++) //注意,对于每个数,只需往一边找,否则会出现重复计算。
cnt += upper_bound(a+i+, a++n, a[i]+mid)-(a+i+); //在[i+1, n+1)的范围,即[i+1,n]
return cnt>=m;
} int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]); sort(a+, a++n);
m = n*(n-)/; //有多少个数
m = (m+)/; //中位数所在的位置
int l = , r = a[n]-a[];
while(l<=r)
{
int mid = (l+r)>>;
if(test(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
}

POJ3579 Median —— 二分的更多相关文章

  1. POJ3579 Median

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

  2. poj3579 二分搜索+二分查找

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5468   Accepted: 1762 Descriptio ...

  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 二分套二分 或 二分加尺取

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

  6. C. Maximum Median 二分

    C. Maximum Median 题意: 给定一个数组,可每次可以选择一个数加1,共执行k次,问执行k次操作之后这个数组的中位数最大是多少? 题解:首先对n个数进行排序,我们只对大于中位数a[n/2 ...

  7. Median(二分+二分)

    Median http://poj.org/problem?id=3579 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  8. 【POJ - 3579 】Median(二分)

    Median Descriptions 给N数字, X1, X2, ... , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤N). 我们能得到 C(N,2) 个 ...

  9. POJ 3579 Median(二分答案)

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

随机推荐

  1. msp430入门学习43

    msp430的其他十一 msp430入门学习

  2. 使用selenium抓取淘宝的商品信息

    淘宝的页面大量使用了js加载数据,所以采用selenium来进行爬取更为简单,selenum作为一个测试工具,主要配合无窗口浏览器phantomjs来使用. import re from seleni ...

  3. Linux编译安装Apache+PHP

    Linux编译安装Apache+PHP 来自:自学it网,http://www.zixue.it/. 1]编译安装Apache+PHP 1.安装程序依赖库和开发环境   为了省事把所需要的库文件全都安 ...

  4. Perl、PHP、Python、Java和Ruby的比较

    提问 ◆ Perl.Python.Ruby和PHP各自有何特点? ◆ 为什么动态语言多作为轻量级的解决方案? ◆ LAMP为什么受欢迎? ◆ Ruby on Rails为什么会流行? ◆ 编程语言的发 ...

  5. JDBC自定义连接池

    开发中,"获得连接"和"释放资源"是非常消耗系统资源的,为了解决此类性能问题可以采用连接池技术来共享连接Connection. 1.概述 用池来管理Connec ...

  6. (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)

    1.配置tomcat数据源: #   数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...

  7. Linux下使用nohup实现在后台运行程序(转)

    相比上一篇http://www.cnblogs.com/EasonJim/p/6833417.html使用screen实现后台运行程序,各有各的好处,多一种选择吧. Linux下一般比如想让某个程序在 ...

  8. Linux服务器同步网络时间

    Linux服务器运行久时,系统时间就会存在一定的误差,一般情况下可以使用date命令进行时间设置,但在做数据库集群分片等操作时对多台机器的时间差是有要求的,此时就需要使用ntpdate进行时间同步. ...

  9. [Unit Testing] Fundamentals of Testing in Javascript

    In this lesson, we’ll get the most fundamental understanding of what an automated test is in JavaScr ...

  10. SQL经常使用语法

    一.基础 1.创建数据库 CREATE DATABASE database-name 2.删除数据库 drop database dbname 3.备份sql server --- 创建 备份数据的 ...