1. 程式人生 > >平面內距離最近的兩個點-- 分治法

平面內距離最近的兩個點-- 分治法

題目地址 hdu 1007

題目描述

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

ac程式碼

#include <cstdio>
#include <memory>
#include <cassert>
#include <cstdlib> #include <cmath> #include <cstring> #include <iostream> #include <string> #include <vector> #include <queue> #include <algorithm> #include <sstream> #include <list> #include <stack> #include <map> #include <set> #include <unordered_map> using namespace std; #define INF 0x7fffffff struct Pos{ double x; double y; }; double Dis(Pos p1, Pos p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y)); } bool cmp(Pos p1, Pos p2) { if(p1.x == p2.x) return p1.y < p2.y; return p1.x < p2.x; } double smaller(double p, double q) { return (p > q) ? q : p; } int n; vector<Pos> v; double b2(int left, int right) { if(left >= right) return 0; if(right == left + 1) return Dis(v[left], v[right]); int mid = (right - left) / 2 + left; double d = smaller( b2(left ,mid), b2(mid, right)); for(int i=mid-1;i>= left&& v[mid].x - v[i].x < d; i--) { for(int j=mid +1;j<=right && v[j].x - v[mid].x < d && fabs(v[i].y - v[j].y) < d; j ++) { double disTmp = Dis(v[i], v[j]); if(disTmp < d) { d = disTmp; } } } return d; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(scanf("%d", &n) != EOF && n != 0) { v.clear(); Pos ptmp; for(int i = 0;i<n;i++) { scanf("%lf%lf", &ptmp.x,&ptmp.y); v.push_back(ptmp); } if(n == 1){ printf("0.00\n"); }else{ sort(v.begin(), v.end(), cmp); double d = b2(0, n-1); printf("%.2lf\n", d * 0.5); } } return 0; }