基爷的中位数

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/contest/show/61

Description

给你N个数,X1,X2,...,XN, 基爷让我们计算任意两个数差的绝对值 ∣Xi−Xj∣ (1≤i<j≤N) 。 这样,我们可以得到 C2N 个数。

现在,基爷希望聪明的你能用一个简单的程序求出这 C2N 个数的中位数!

Input

输入有多组数据。

每组数据,第一行一个整数 N,第二行给出 N 个整数 X1,X2,...,XN ( |Xi|≤1,000,000,000; 3≤N≤100,000 )

Output

按要求输出中位数,每个数占一行。

Sample Input

4
1 3 2 4
3
1 10 2

Sample Output

1
8

HINT

当这 C2N 个数的个数为偶数 M 的时候,取第 ⌊M2⌋ 个最小的数作为中位数 ( 别问为什么,这就是基爷的中位数! )

题意

题解:

二分答案枚举中位数,然后再二分 a[i]-a[j]=二分的那个数的值在哪儿

nloglog的做法

nlog就要用two pointer去优化

代码:

  1. //qscqesze
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <sstream>
  11. #include <queue>
  12. #include <typeinfo>
  13. #include <fstream>
  14. #include <map>
  15. #include <stack>
  16. typedef long long ll;
  17. using namespace std;
  18. //freopen("D.in","r",stdin);
  19. //freopen("D.out","w",stdout);
  20. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  21. #define maxn 200001
  22. #define mod 10007
  23. #define eps 1e-9
  24. int Num;
  25. char CH[];
  26. //const int inf=0x7fffffff; //нчоч╢С
  27. const int inf=0x3f3f3f3f;
  28. /*
  29.  
  30. inline void P(int x)
  31. {
  32. Num=0;if(!x){putchar('0');puts("");return;}
  33. while(x>0)CH[++Num]=x%10,x/=10;
  34. while(Num)putchar(CH[Num--]+48);
  35. puts("");
  36. }
  37. */
  38. inline ll read()
  39. {
  40. int x=,f=;char ch=getchar();
  41. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  42. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  43. return x*f;
  44. }
  45. inline void P(int x)
  46. {
  47. Num=;if(!x){putchar('');puts("");return;}
  48. while(x>)CH[++Num]=x%,x/=;
  49. while(Num)putchar(CH[Num--]+);
  50. puts("");
  51. }
  52. //**************************************************************************************
  53.  
  54. ll a[maxn],n;
  55. ll tmp;
  56.  
  57. int main()
  58. {
  59. while(scanf("%lld",&n)!=EOF)
  60. {
  61. for(int i=;i<n;i++)
  62. scanf("%lld",&a[i]);
  63. tmp=n*(n-)/;
  64. sort(a,a+n);
  65. ll l=,r=;
  66. while(l+<r)
  67. {
  68. ll mid=(l+r)>>;
  69. ll ans=;
  70. for(int i=;i<n;i++)
  71. {
  72. ans+=a+n-lower_bound(a+i+,a+n,a[i]+mid);
  73. }
  74. if(ans<=tmp/)
  75. r=mid;
  76. else
  77. l=mid;
  78. }
  79. printf("%lld\n",l);
  80. }
  81.  
  82. }

2015 UESTC 搜索专题D题 基爷的中位数 二分的更多相关文章

  1. 2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS

    基爷与加法等式 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...

  2. 2015 UESTC 搜索专题N题 韩爷的梦 hash

    韩爷的梦 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descrip ...

  3. 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜

    Eight Puzzle Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...

  4. 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp

    秋实大哥の恋爱物语 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...

  5. 2015 UESTC 搜索专题E题 吴队长征婚 爆搜

    吴队长征婚 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descri ...

  6. 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索

    邱老师降临小行星 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

  7. 2015 UESTC 搜索专题M题 Palindromic String 马拉车算法

    Palindromic String Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/s ...

  8. 2015 UESTC 搜索专题J题 全都是秋实大哥 kmp

    全都是秋实大哥 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...

  9. 2015 UESTC 搜索专题A题 王之迷宫 三维bfs

    A - 王之迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

随机推荐

  1. Installation Guide for Appium 1.6.3

    A.) System Requirements : - Require node 4 or above Xcode 8 iOS 10 B.) Open terminal and type follow ...

  2. mysql中列的增删改

    增加列: ); ) after id; ) first; 修改列名: ); #change可改名字与字段类型 mysql> alter table a change uid uid int; Q ...

  3. 关于linux系统如何实现fork的研究(二)【转】

    转自:http://www.aichengxu.com/linux/7166015.htm 本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 前一篇关于li ...

  4. Oracle常用sql语句(三)之子查询

    子查询 子查询要解决的问题,不能一步求解 分为: 单行子查询 多行子查询 语法: SELECT select_list FROM table WHERE expr operator (SELECT s ...

  5. EL(表达式)语言的几种运算符

    1.EL的基本语法 (1)EL表达式语法:以${开头,以}结束,中间为合法的表达式,具体语法格式如下: ${expression} (2)参数说明:Expression:指定要输出的内容,可以是字符串 ...

  6. interesting Integers(数学暴力||数论扩展欧几里得)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8

  7. Linux 基础——文件搜索命令find

    一.find命令的好处 有时会经常在目录下找文件或目录的具体存放在哪,但是该目录下的文件又很多不好找出.这时并不需要手动查看所有的文件,用find命令来帮助查找就行了.所以文件或目录一定归好类,存放有 ...

  8. Nuxt 2.0 需要将pug-loader改成pug-plain-loader

    Nuxt 2.0 需要将pug-loader改成pug-plain-loader npm i pug-plain-loader -D 解决问题!! 参考链接 https://my.oschina.ne ...

  9. eclipse svn 以一种访问权限不允许的方式做了一个访问套接字的尝试

    以一种访问权限不允许的方式做了一个访问套接字的尝试 svn: Unable to connect http://xxx.xxx 安装插件是把Eclipse的网络访问禁止了,然后用svn就老提示[以一种 ...

  10. 提起Ajax请求的方式(POST)

    前言 => 是ES6中的arrow function x=>x+6 就相当于 function(x){ return x+6; } 正文 XMLHttpRequest a=new XMLH ...