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

 
 
正解:二分答案
解题报告:
  今天考试的T2,考场上面还想了很久XD
  因为是求处在中位数的差值,所以直接二分这个差值x,对于每个数查找整个数列有多少个比他大x以上的数有多少直接checke检查合法性。
 
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
#define RG register
const int MAXN = ;
int n;
LL a[MAXN];
LL l,r,ans,ans2;
LL N,cnt,zhong; inline int getint()
{
RG int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline bool check(LL x){
cnt=; LL now;
for(RG int i=;i<=n;i++) {
now=lower_bound(a+i,a+n+,a[i]+x)-a;
cnt+=(LL)n-now+;
}
if(cnt>zhong) return true; return false;
} inline void work(){
while(scanf("%d",&n)!=EOF) {
for(RG int i=;i<=n;i++) a[i]=getint();
sort(a+,a+n+); l=; r=a[n]-a[]+; a[n+]=(<<);
N=(LL)n*(n-)/; LL mid;
zhong=N/; ans=;
while(l<=r) {
mid=(l+r)/;
if(check(mid)) ans=mid,l=mid+;
else r=mid-;
}
printf("%lld\n",ans);
}
} int main()
{
work();
return ;
}

POJ3579 Median的更多相关文章

  1. POJ3579 Median —— 二分

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

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

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

  3. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  4. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  5. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  6. Applying vector median filter on RGB image based on matlab

    前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...

  7. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  8. Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing

    B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...

  9. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

随机推荐

  1. svn分支开发与主干合并(branch & merge)

    下面我将step by step地演示如何一次完整的branching和merging,包括创建分支.分支开发.分支和主线同步,分支合并到主线的全过程,甚至包括如何在本地创建一个测试用的reposit ...

  2. Properties集合的练习

    1.更改文件中的数据,特定键的值: 需求:我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么. 请写一个程序判断是否有"lisi"这样的键存在,如 ...

  3. Android SQLite (四 ) 全面详解(二)

    SQLite创建数据库 创建数据库语法: sqlite3 DatabaseName.db 如下展示一个实例: SQLite附加数据库 假设这样一种情况,当在同一时间有多个数据库可用,您想使用其中的任何 ...

  4. Windows server 2008系统基本优化

    前几天用上了Windows server 2008刚开始还真不习惯,毕竟是做服务器用的系统和娱乐操作系统有很大区别.先总结几点,以后慢慢更新 安装Win2008驱动: 大部分支持vista系统的驱动都 ...

  5. poj 1163 The Triangle

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43809   Accepted: 26430 De ...

  6. Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别

    Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...

  7. ibatis动态查询条件

    ibatis的调试相对困难,出错的时候主要依据是log4生成的log文件和出错提示,这方面要能比较熟练的看懂. 下面这个配置基本上包含了最复杂的功能:分页\搜索\排序\缓存\传值Hash表\返回has ...

  8. Windows下MemCache多端口安装配置

    Windows下MemCache环境安装配置的文章很多,但大部分都是用的默认端口11211,如何修改默认端口.如何在一台服务器上配置多个MemCache端口?这正式本文要解决的问题. 1.从微软官网下 ...

  9. 记一次w3wp占用CPU过高的解决过程(Dictionary和线程安全)

    项目上线以来一直存在一个比较揪心的问题,和一个没有信心处理的BUG,那就是在应用程序启动时有可能会导致cpu跑满99%或持续在一个值如50%左右,这样一来对服务器的压力是非常大的,经常出现服务器无法远 ...

  10. 疯狂位图之——位图生成12GB无重复随机乱序大整数集

    上一篇讲述了用位图实现无重复数据的排序,排序算法一下就写好了,想弄个大点数据测试一下,因为小数据在内存中快排已经很快. 一.生成的数据集要求 1.数据为0--2147483647(2^31-1)范围内 ...