Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3528   Accepted: 1001

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

 
二分中位数
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define maxn 100005
#define INF 200005
typedef long long ll; int n;
int a[maxn],dis[maxn];
ll num; bool judge(int x) { int pos,i = ,now = ;
ll sum = ;
while(i < n) {
pos = upper_bound(dis + ,dis + n + ,x + now) - dis;
sum += n - (pos);
now = dis[i];
if((n - ) - pos + == ) break;
++i;
} //printf("x = %d sum = %lld\n",x,sum); return num - sum - (num % ) >= sum;
} void solve() { num = (ll)n * (n - ) / ; int l = INF,r = a[n] - a[];
for(int i = ; i < n; ++i) {
dis[i] = a[i + ] - a[];
l = min(l,a[i + ] - a[i]);
// printf("%d ",dis[i]);
}
dis[n] = INF; //printf("l = %d r = %d\n",l,r); while(l < r) {
int mid = (l + r) >> ;
if(judge(mid)) {
r = mid;
} else {
l = mid + ;
}
} printf("%d\n",l); } int main()
{
// freopen("sw.in","r",stdin); while(~scanf("%d",&n)) {
for(int i = ; i <= n; ++i) {
scanf("%d",&a[i]);
} sort(a + ,a + n + ); solve();
} return ;
}

最新文章

  1. Android之SQLite数据存储
  2. java中的注解(Annotation)
  3. C程序中对时间的处理——time库函数详解
  4. 怎么解决Android studio导入项目卡死
  5. timeSeries db之:使用Metrics监控应用程序的性能 (zz)
  6. (转载)OC学习篇之---类的三大特性:封装,继承,多态
  7. CASS转ARCGIS
  8. HTML5学习(五)----SVG
  9. Ext.Net学习笔记18:Ext.Net 可编辑的GridPanel
  10. MVC零基础学习整理(一)
  11. mysql数据库事务详细剖析
  12. windows平台下cocos2d-x-3.0beta2创建新项目
  13. MySQL的一些常用的SQL语句整理
  14. IDEA破解 Intellij IDEA license server 激活(可用)
  15. linux安装redis操作
  16. 阿里面试题BIO和NIO数量问题附答案和代码
  17. repr方法字符串输出实例对象的值
  18. ASP.NET MVC 5 开发环境配置
  19. Maven配置本地仓库
  20. poj2299——Ultra-QuickSort

热门文章

  1. qt QLabel 显示网络图片
  2. ROS
  3. CSS精粹之布局技巧
  4. 【WinForm】线程中向listview添加数据
  5. Mysql多表查询(两张独立表,一张关系表)
  6. 关于Protobuf在游戏开发中的运用
  7. Windows下关于Composer使用时出现的问题及解决办法
  8. require.js入门指南(三)
  9. 查看SQL Server 备份信息
  10. OBIEE 11g:Error:nQSError 36010 Server version 318 cannot read the newer version of the repository