分治法求平面最近点对
考虑对所有点按照横坐标中位数进行分治,假设(d=min(solve(l,mid),solve(mid+1,r)))
再想想怎么合并跨越中线的点
我们发现暴力最坏的情况是((frac{n}{2})^2)根本不能接受
我们来大力减枝
我们发现如果两个点的某一坐标差超过了(d)显然不可能对答案有贡献,所以我们只需要考虑横坐标在([a[mid].x-d,a[mid].x+d])范围内的点
同时纵坐标之差也不会超过(d),所以每个点可能计算的点对在(d*2d)的矩形内
我们考虑把矩形6等分成(frac{d}{2}*frac{2d}{3})的矩阵,那么小矩阵对角线的长度是(frac{5d}{6})
这说明不可能有两个点在同一小矩形内,所以每个点最多只需要与(6)个点互相匹配
只要实现的优雅。。复杂度就是(O(nlogn))
当然我们对(y)坐标进行排序的时候不能大力(sort),可以利用分治结构归并排序保证复杂度