Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5468   Accepted: 1762

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个数,任意两个数之间的差共有m=(n-1)*n/2种然后让你输出这些数中间的那一个,规则为
若m为奇数,中间的那一个为第(m+1)/2小的数,若m为为偶数,中间那个数指的是第m/2小的数。
思路分析:看了一下数据范围,如果用最正常最简单的做法,复杂度O(n^2),肯定会超时,因此需要采用高效率的
二分算法,因为a[i]-a[j]是取了绝对值的,因此就相当于是大的减小的,因此可以将数组a[n]先sort排序,然后
根据差值确定二分范围,我确定的是0~a[n-1]-a[0],然后开始二分搜索,而check函数则主要是统计比a[i]+d小的
数有多少个,如果>=(m+1)/2就return true else return false,而在统计的过程中如果用传统的顺序查找肯
定也会到致超时,所以应该使用高效率的二分查找,我直接用了upper_bound函数,统计的时候注意upper_bound-a
是所有<=a[i]+x的数组元素的个数,应该减去所有<=a[i]的元素(共i+1个)
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100000+5];
int n;
int temp;
bool check(int x)
{
    int cnt = 0;
    for(int i=0; i<n; i++)
    {
       int t=upper_bound(a,a+n,a[i]+x)-a;//比a[i]+x小的元素的个数
       cnt+=(t-i-1);//排除a[i]之前的那些元素,共有i+1;
    }
    if(cnt>=temp) return true; 
        else return false;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
         for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
         sort(a,a+n);
         int m=n*(n-1)/2;
         temp=(m+1)/2;
         int l=0,r=a[n-1]-a[0];
         int ans;
         while(l<=r)//二分搜索
         {
             int mid=(l+r)>>1;
             if(check(mid))
              ans=mid,r=mid-1;
             else l=mid+1;
         }
         printf("%d\n",ans);
    }
    return 0;
}

poj3579 二分搜索+二分查找的更多相关文章

  1. c#-二分查找-算法

    折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...

  2. 二分查找算法java实现

    今天看了一下JDK里面的二分法是实现,觉得有点小问题.二分法的实现有多种今天就给大家分享两种.一种是递归方式的,一种是非递归方式的.先来看看一些基础的东西. 1.算法概念. 二分查找算法也称为折半搜索 ...

  3. [转]编程珠玑第五章二分搜索(折半查找)之java实现

    http://blog.csdn.net/hwe_xc/article/details/51813080 二分搜索又称为折半查找,用来高效快速的解决如下问题: 我们需要确定排序后的数组x[0..n-1 ...

  4. 【algorithm】 二分查找算法

    二分查找算法:<维基百科> 在计算机科学中,二分搜索(英语:binary search),也称折半搜索(英语:half-interval search)[1].对数搜索(英语:logari ...

  5. 递归分治算法之二维数组二分查找(Java版本)

    [java] /** * 递归分治算法学习之二维二分查找 * @author Sking 问题描述: 存在一个二维数组T[m][n],每一行元素从左到右递增, 每一列元素从上到下递增,现在需要查找元素 ...

  6. C++ STL中的Binary search(二分查找)

    这篇博客转自爱国师哥,这里给出连接https://www.cnblogs.com/aiguona/p/7281856.html 一.解释 以前遇到二分的题目都是手动实现二分,不得不说错误比较多,关于返 ...

  7. 【学习记录】二分查找的C++实现,代码逐步优化

    二分查找的思想很简单,它是针对于有序数组的,相当于数组(设为int a[N])排成一颗二叉平衡树(左子节点<=父节点<=右子节点),然后从根节点(对应数组下标a[N/2])开始判断,若值& ...

  8. Search for a Range——稍微升级版的二分查找

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  9. Python 算法之二分查找

    二分查找 二分查找又称折半查找 优点是比较次数少,查找速度快,平均性能好 缺点是要求待查表为有序表,且插入删除困难 折半查找方法适用于不经常变动而查找频繁的有序列表. 猜数字游戏 1.生成一个有序列表 ...

随机推荐

  1. web标准(复习)--3 二列和三列布局

    今天学习二列和三列布局,将涉及到以下内容和知识点 二列自适应宽度 二列固定宽度 二列固定宽度居中 xhtml的块级元素(div)和内联元素(span) float属性 三列自适应宽度 三列固定宽度 三 ...

  2. java练习-滚动文字

    <marquee direction="left" onMouseOver="this.scrollAmount=5" onMouseOut=" ...

  3. 有时summary的状态和details是否有open属性有关

    用过mac的同学对这个界面一定很熟悉,因为这个界面和我们今天要说的details有很多相近的地方,首先,其有折叠效果,用户可以自己选择打开或关闭哪一个,其次,当我们直接打开的时候,默认会有几个标签是打 ...

  4. FJ省队集训DAY5 T1

    思路:考试的时候打了LCT,自以为能过,没想到只能过80.. 考完一想:lct的做法点数是100W,就算是nlogn也会T. 讲一下lct的做法把:首先如果一条边连接的两个点都在同一个联通块内,那么这 ...

  5. Unity脚本的生命周期中几个重要的方法

    1.function Update () {} 正常更新,用于更新逻辑.此方法每帧都会由系统自动调用一次.2.function LateUpdate () {} 推迟更新,此方法在Update() 方 ...

  6. MySQL常用时间函数

    官方文档:Date and Time Functions Name Description ADDDATE() Add time values (intervals) to a date value ...

  7. Hash Map (Hash Table)

    Reference: Wiki  PrincetonAlgorithm What is Hash Table Hash table (hash map) is a data structure use ...

  8. Abstract Methods and Classes

    阅读了Java的官方Doc,在此总结如下. Abstract Class & Method In jave, "abstract" can be a modifier to ...

  9. [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...

  10. the process cannot access the file because it is being used by another process

    当在IIS中改动绑定的port号后启动时遇到例如以下错误,表明你的port号已经被占用了 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdmljMDIyOA ...