Blackops

初心易得,始终难守

0%

HDU 1007 Quoit Design(分治法求最近点对)

Quoit Design

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55702 Accepted Submission(s): 14685

Problem Description
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.

Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.

Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.

Sample Input
2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0

Sample Output
0.71
0.00
0.75

题目链接:HDU 1007
题意就是叫你求平面最近点对距离,答案是最近距离除以$2$。由于上课老师又讲了这个决定重新看一下。
首先这题分治两边很简单,递归就行了,如果最短距离是左右两边的,则需要选出一些可能的点集合,怎么选呢?就选情况最差的,离中间点$x$距离不超过左右递归得到的答案最小值$d$的,然后由于鸽巢原理,这样选出来之后$n^2$暴力求解,但是由于鸽巢原理,对于一个点最多只会有剩下的六个点能跟它组成更优的解,随机了$1e5$个整数点测试了一下,一个点最多匹配的次数是$0$、$1$、$2$,匹配$6$个点的只出现了$189$次。


实际上每次用$sort$对筛选出来的点按$y$轴排序,复杂度是$O(N{log^2}N)$的,因此可以在分治的时候用归并排序顺便把点按照$y$轴排序掉,这样复杂度才是$O(NlogN)$
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct Point
{
double x, y;
} p[N], temp[N];

inline double dis(const Point &a, const Point &b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
inline bool cmpx(const Point &a, const Point &b)
{
return a.x < b.x;
}
double solve(int l, int r)
{
if (l >= r)
return 1e9;
else
{
int mid = MID(l, r);
Point &mp = p[mid];//mid不能变,要先拿出来
double d = min(solve(l, mid), solve(mid + 1, r));
//begin
int L = l, R = mid + 1, X = l;
while (L <= mid && R <= r)
{
if (p[L].y < p[R].y)
temp[X++] = p[L++];
else
temp[X++] = p[R++];
}
while (L <= mid)
temp[X++] = p[L++];
while (R <= r)
temp[X++] = p[R++];
for (int i = l; i <= r; ++i)
p[i] = temp[i];
//end
int sz = 0;
for (int i = l; i <= r; ++i)
if (fabs(p[i].x - mp.x) <= d)
temp[sz++] = p[i];
for (int i = 0; i < sz; ++i)
{
for (int j = i + 1; j < sz; ++j)
{
if (temp[j].y - temp[i].y >= d)
break;
d = min(d, dis(temp[j], temp[i]));
}
}
return d;
}
}
int main(void)
{
int n, i;
while (~scanf("%d", &n) && n)
{
for (i = 0; i < n; ++i)
scanf("%lf%lf", &p[i].x, &p[i].y);
sort(p, p + n, cmpx);
printf("%.2f\n", solve(0, n - 1) / 2);
}
return 0;
}