P7883 O(n) randomized algorithm
一个真的随机算法:
A simple randomized sieve algorithm for the closest-pair problem (https://www.cs.umd.edu/~samir/grant/cp.pdf)
- 循环直到删完所有点:
- 随机选一个点,计算它到所有点的最短距离
d 。 - 将所有点划分到
l = d/3 的网格里,比如\left (\lfloor \frac x l \rfloor, \lfloor \frac y l \rfloor \right ) 。 - 将九宫格内孤立的点删除,这意味着这些点的最近点对距离不小于
\frac {2 \sqrt 2} {3} d ,其中\frac {2 \sqrt 2} 3 < 1 。
- 随机选一个点,计算它到所有点的最短距离
- 取最后一个
d ,将所有点划分到\left (\lfloor \frac x d \rfloor, \lfloor \frac y d \rfloor \right ) 的网格里,暴力计算九宫格内的答案。
第一部分每次期望会删掉至少一半的点,因为有
第二部分分析类似分治做法,周围只有常数个点。
所以总复杂度是