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
Author
CHEN, Yue
题意:
给出很多个点,求最近的两点
思路:
求平面最近点对。普通的O(n^2)暴力比较肯定会超时,所以应用分治思想。
先将所有点在数轴上依x轴大小进行排序,在区间内不断进行分治对比。
#pragma once
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <iterator>
#include <sstream>
#include <fstream>
#include <list>
#define MST(a, b) memset(a, b, sizeof a);
using namespace std;
typedef long long ll;
const int MAXN = 100000 + 100;
const int INF = 0x3f3f3f3f;
struct node {
double x, y;
} nd[MAXN], tmpnd[MAXN];
bool cmp_x(node a, node b) {
if (a.x != b.x) return a.x < b.x;
else return a.y < b.y;
}
bool cmp_y(node a, node b) {
return a.y < b.y;
}
double dis(node a, node b) {
return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
}
double solve(int left, int right) {
double d = INF;
if (left == right) return d;
if (left + 1 == right) return dis(nd[left], nd[right]);
int mid = (left + right) / 2;
double d1 = solve(left, mid), d2 = solve(mid + 1, right);
d = min(d1, d2);
int k = 0;
for (int i = left; i <= right; i++)
if (fabs(nd[mid].x - nd[i].x) <= d) tmpnd[k++] = nd[i];
sort(tmpnd, tmpnd + k, cmp_y);
for (int i = 0; i < k; i++)
for (int j = i + 1; j < k && tmpnd[j].y - tmpnd[i].y < d; j++)
d = min(d, dis(tmpnd[i], tmpnd[j]));
return d;
}
int main() {
int n;
while (scanf("%d", &n), n) {
MST(nd, 0); MST(tmpnd, 0);
for (int i = 0; i < n; i++) scanf("%lf %lf", &nd[i].x, &nd[i].y);
sort(nd, nd + n, cmp_x);
printf("%.2lf\n", solve(0, n - 1) / 2);
}
}