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. 重绘DataGridView的DataGridViewCheckBoxCell控件

    最近项目中要用到在DataGridView单元格里面放置一个带有文本的 DataGridViewCheckBoxCell控件但原有 的是不支持的然后我就想着重写个 DataGridViewCheckB ...

  2. CNN结构:色彩特征提取-从RGB空间到HSV空间(色彩冷暖判断)

      转自知乎和百度百科:从零开始学后期             文章: 冷暖色区分?冷暖肤色适用于那些色系的彩妆?    文章:干货 |如何判断人体色冷暖?如何判断色彩冷暖?(值得收藏研读!) -蒜苗 ...

  3. 调用CAD内的颜色选择对话框

    colordialog类 int color; acedSetColorDialog(color,TRUE,0); 第一个函数返回的是颜色的RGB值

  4. EF入门

    1.(安装EF)右键项目

  5. pandas - 案例(股票分析)

    需求: 使用tushare包获取某股票的历史行情数据. 输出该股票所有收盘比开盘上涨3%以上的日期. 输出该股票所有开盘比前日收盘跌幅超过2%的日期. 假如我从2010年1月1日开始,每月第一个交易日 ...

  6. debian 7 安装

    1. 从 live cd 或者 dvd 进行安装,官方文档已经够用,可以将 live cd 写进 u 盘,也可以将 dvd 放到硬盘上通过 grub 引导启动安装,文档在这里. 2.安装过程中尽量选择 ...

  7. WPF Style设置和模板化Template

    WPF样式设置和模板化是一套功能(样式,模板,触发器和演示图版),可以为产品设置统一外观.类似于html的css,可以快速的设置一系列属性值到控件. 案例:ButtonStyle 这里创建了一个目标类 ...

  8. 个人学习记录--取表中Name相同的最大值,非Group By,可延伸

    ), qy ), je INT); INSERT INTO @t SELECT '产品一', '北京', UNION ALL SELECT '产品一', '上海', UNION ALL SELECT ...

  9. nagios新增监控集群、卸载监控集群批量操作

    1.一定要找应用侧确认每台节点上需要监控的进程,不要盲目以为所有hadoop集群的zk.journal啥的都一样,切记! 2.被监控节点只需要安装nagios-plugin和nrpe,依赖需要安装xi ...

  10. HDU 1836 畅通工程

    畅通工程 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 18636 ...