Median

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9201 Accepted: 3209

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


中文题意:

给N数字, X1, X2, … , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤ N). 我们能得到 C(N,2) 个差值,现在我们想得到这些差值之间的中位数。

如果一共有m个差值且m是偶数,那么我们规定中位数是第(m/2)小的差值。

输入包含多测
每个测试点中,第一行有一个NThen N 表示数字的数量。
接下来一行由N个数字:X1, X2, … , XN
( Xi ≤ 1,000,000,000 3 ≤ N ≤ 1,00,000 )


解题心得:

  1. 首先要知道的是n个数字可以得到C(N,2)个数字,那么要得到中位数,如果是偶数就要得到第n/2大的数字,如果是奇数就要得到第n/2+1个数字。
  2. 具体做法可以先将给出的数字排序,然后每次二分枚举一个中位数(O(logn)),然后验证比这个中位数小的数字有多少个,在验证比这个中位数小的数字有多少个的时候可以选择尺取法(具体实现看代码,O(n)),这样就在O(nlogn)的复杂度内完成。

#include <algorithm>
#include <stdio.h>
#include <cstring>
using namespace std;
const int maxn = 1e5+100;int n,num[maxn];
long long tot;void init() {tot = 0;tot = (long long)n*(n-1)/2;//差值的个数if(tot%2 == 0) {//中位数在第几个tot /=2;}elsetot = tot/2 + 1;for(int i=0;i<n;i++) {scanf("%d",&num[i]);}sort(num,num+n);
}bool checke(int va) {int t = 0;long long sum = 0;for(int i=0;i<n;i++) {while(t < n && num[t] - num[i] <= va)//尺取法看比va小的值有多少个t++;sum += t-i-1;}if(sum < tot)return true;return false;
}int binary_search() {//每次枚举一个中位数的值int l,r;l = 0, r = 1e9+100;while(r - l > 1) {int mid = (l + r) >> 1;if(checke(mid))l = mid;elser = mid;}return r;
}int main() {while(scanf("%d",&n ) != EOF) {init();int ans = binary_search();printf("%d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/GoldenFingers/p/9107120.html

POJ:3579-Median(二分+尺取寻找中位数)相关推荐

  1. (POJ - 3579)Median(二分)

    中文题意:现在有N个神秘的数字(A1,A2,A3-.An),我们需要从这串数字中得到一个密码. 得到密码的方式为: 先计算每个数字之间的差:|Ai-Aj|(1<=i<j<=N),最后 ...

  2. POJ 3579 Median

    题目链接 Description Given N numbers, X1, X2, - , XN, let us calculate the difference of every pair of n ...

  3. poj 3579 Median 中间值(二分搜索)

    Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numbers: ∣Xi - X ...

  4. 【CodeForces - 514D】R2D2 and Droid Army(二分+滑动窗口ST表,或 尺取+单调队列或STLmultiset)

    题干: An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., a ...

  5. POJ 3320 Jessica's Reading Problem (尺取)

    Description 给出一段长度为\(n\)的整数序列,求包含所有出现过的数的连续区间的最小长度. Input 第一行给出序列长度\(n\),第二行给出\(n\)个整数,表示这个序列.\(n \l ...

  6. CF1547F Array Stabilization (GCD version) st表 + 尺取/二分

    传送门 题意: 思路: 容易发现,我们将所有aaa都除上所有aaa的gcdgcdgcd,实际上就是让你求一个最小的lenlenlen,对于所有iii,gcd(ai,ai+1,...,ai+len−1) ...

  7. 【Gym - 101608G】WiFi Password (区间或,线段树 或 按位处理+尺取 或 二分)

    题干: Just days before the JCPC, your internet service went down. You decided to continue your trainin ...

  8. 【CodeForces - 660C】Hard Process (尺取 或 二分+滑窗,前缀和预处理)

    题干: You are given an array a with n elements. Each element of a is either 0 or 1. Let's denote the l ...

  9. POJ-3061 Subsequence 二分或尺取

    题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...

最新文章

  1. 批量 杀掉 mysql 连接_批量杀死MySQL连接的四种方法详解
  2. CentOS 6.8 安装 RabbitMQ
  3. Repeater分页
  4. Java用Java编译
  5. c++语言中break的作用,C++ break和continue用法详解
  6. 一个WordPress站点绑定多个域名
  7. linux替换指定行指定列的内容,linux – sed替换特定行号的特定列号值
  8. 从前端到“钱”端,优秀程序员要掌握哪些核心技术?
  9. fpga运算服务器_SparseArray替代HashMap来提高性能
  10. 二叉树的创建及各种遍历
  11. 量子计算机与GIS,量子计算机系列---开篇,原理
  12. 关于数字签名驱动解决方法
  13. 华硕主板如何用u盘启动计算机,华硕主板怎么设置u盘启动 华硕主板设置u盘启动方法【图文】...
  14. 刘强东第二次“二次创业”
  15. 你有一份七夕赚钱指南等待签收
  16. 男人最碰不得的十大危险女人
  17. ul、li 标签实现完美表格
  18. /deep/ 在谷歌浏览器89+版本失效问题解决
  19. [RK3288][Android6.0] WiFi之PNO功能了解
  20. 如何系统的学习服务器相关知识?

热门文章

  1. zookeeper 运维
  2. Spring AOP原理及拦截器
  3. selenium如何解决IE自动填充表单问题
  4. Hashtable源码注释
  5. 2010.9.29 今日问题
  6. 一个特别棒的远程在线学习方法的分享!!!妈妈再也不怕我的网课录屏啦学不进去啦!!!
  7. C语言增量内存申请 realloc
  8. 具有只读属性的ComboBox
  9. DevOps简单介绍
  10. 你真的会使用assert吗?