Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7423   Accepted: 2538

Description

Given N numbers, X1, X2, ... , 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 m = 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 X1, X2, ... , XN, ( Xi ≤ 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

题意:一个序列  他们的差值的绝对值产生一个新的序列  问这个序列的中位数是那个
二分答案 
判断的地方我们也需要用二分 假如 中位数是x的话  我们判断最开始的那个数组a
假如我们知道a[i]+x在a数组的位置 那么我就知道 和a[i]的差值小于等于x的个数为n-t个 我们就可以知道有多少个数是小于这个x的 
 
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
int a[N];int n,m;
int judge(int x){
int sum=;
for(int i=;i<n;i++){
int t=lower_bound(a,a+n,a[i]+x)-a;
// 存在差值 x
sum=sum+n-t;// 和a[i]的差值小于等于x的个数为n-t;
}
if(sum>m)return ;
else{
return ;
}
}
int main(){ while(scanf("%d",&n)!=EOF){
int maxx=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
m=n*(n-)/;
int low=;
int ans;
int high=a[n-];
while(low<=high){
int mid=(low+high)>>;
if(judge(mid)){
ans=mid;
low=mid+;
}
else
high=mid-;
}
cout<<ans<<endl;
}
}

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

  1. POJ 3579 Median 二分加判断

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

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

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

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

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

  4. POJ 3579 Median(二分答案)

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

  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. POJ 3579 3685(二分-查找第k大的值)

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

  9. POJ3579 Median —— 二分

    题目链接:http://poj.org/problem?id=3579 Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

随机推荐

  1. Android Studio 快捷键整理

    Alt+回车 导入包,自动修正 Ctrl+N   查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如ge ...

  2. RTL Compiler之synthesis flow

    1 generic RTL Compiler work flow 2 invoking RTL compiler RTL Compiler is invoked from the operating ...

  3. dubbo之令牌验证

    防止消费者绕过注册中心访问提供者 在注册中心控制权限,以决定要不要下发令牌给消费者 注册中心可灵活改变授权方式,而不需修改或升级提供者 可以全局设置开启令牌验证 <!--随机token令牌,使用 ...

  4. tac

    功能说明:反向显示文件内容. 参数选项: -b 在行前而非行尾加分隔标志. -r 将分隔标志视作正则表达式来解析. -s 使用指定字符串代替换行作为分隔标志.   cat命令与tac命令的对比:

  5. CAD在网页中返回当前图纸的最小外包矩形框

    主要用到函数说明: _DMxDrawX::GetMcDbDatabaseBound 返回当前图纸的最小外包矩形框,详细说明如下: 参数 说明 DOUBLE* pLbx 返回最小外包矩形框左下角X值 D ...

  6. js移动端 可移动滑块

    document.addEventListener('DOMContentLoaded', function() { //动态创建回到首页dom var backDom = document.crea ...

  7. Codeforces Round #469 Div. 2题解

    A. Left-handers, Right-handers and Ambidexters time limit per test 1 second memory limit per test 25 ...

  8. vim使用配置-python

    安装vundle git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim 添加配置文件 vim ~/.v ...

  9. Flask - 路由系统

    目录 Flask - 路由系统 @app.route()装饰器中的常用参数 methods : 当前 url 地址,允许访问的请求方式 endpoint:反向url地址,默认为视图函数名(url_fo ...

  10. 8.1.3 Row对象

    假设数据以下面的方式创建并插入数据: import sqlite3 conn = sqlite3.connect(r'D:\test.db') c = conn.cursor() c.execute( ...