HDU 1007 Quoit Design

Quoit Design

Time Limit: 5000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 1007
64-bit integer IO format: %I64d      Java class name: Maina
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

Source

 
解题:最近点对问题,利用分治。
 
其实就是归并排序,先按y排序,然后把按y排序的数组在递归的过程中按x排序,最后又恢复成按y排序。是不是有点像划分树。。。
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100005;
 4 const int INF = 0x3f3f3f3f;
 5 struct Point{
 6     double x,y;
 7     int index;
 8     Point(double tx = 0,double ty = 0,int z = 0){
 9         x = tx;
10         y = ty;
11         index = z;
12     }
13 }Bx[maxn],By[maxn],Bz[maxn],tmp[maxn];
14 bool cmpx(const Point &a,const Point &b){
15     return a.x < b.x;
16 }
17 bool cmpy(const Point &a,const Point &b){
18     return a.y < b.y;
19 }
20 double calc(const Point &a,const Point &b){
21     return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
22 }
23 double closet(int low,int high){
24     double ret = INF;
25     if(low + 2 >= high){
26         for(int i = low; i <= high; ++i)
27             for(int j = i + 1; j <= high; ++j)
28                 ret = min(ret,calc(Bx[i],Bx[j]));
29         return ret;
30     }
31     int mid = (low + high)>>1,l = low,r = mid+1,s = low,cnt = low;
32     for(int i = low; i <= high; ++i)
33         if(By[i].index <= mid) Bz[l++] = By[i];
34         else Bz[r++] = By[i];
35     for(int i = low; i <= high; ++i) By[i] = Bz[i];
36     ret = min(closet(low,mid),closet(mid+1,high));
37     for(l = low,r = mid+1; l <= mid && r <= high;)
38         if(Bz[l].y < Bz[r].y) tmp[s++] = Bz[l++];
39         else tmp[s++] = Bz[r++];
40     while(l <= mid) tmp[s++] = Bz[l++];
41     while(r <= high) tmp[s++] = Bz[r++];
42     for(int i = low; i <= high; ++i) By[i] = Bz[i] = tmp[i];
43     for(int i = low; i <= high; ++i)
44         if(fabs(Bz[i].x - Bz[mid].x) < ret) tmp[cnt++] = Bz[i];
45     for(int i = low; i < cnt; ++i)
46         for(int j = i + 1; j < cnt && Bz[j].y - Bz[i].y < ret; ++j)
47             ret = min(ret,calc(Bz[i],Bz[j]));
48     return ret;
49 }
50 int main(){
51     int n;
52     while(scanf("%d",&n),n){
53         for(int i = 0; i < n; ++i)
54             scanf("%lf %lf",&Bx[i].x,&Bx[i].y);
55         sort(Bx,Bx+n,cmpx);
56         for(int i = 0; i < n; ++i)
57             By[i] = Point(Bx[i].x,Bx[i].y,i);
58         sort(By,By+n,cmpy);
59         printf("%.2f
",closet(0,n-1)/2);
60     }
61     return 0;
62 }
View Code