HDU-1007(最近点对)

Quoit Design

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


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

 

题意:套圈游戏,n个奖品,分别给出坐标,想做个圈,保证圈每次至多套住一个奖品,求圈的最大半径。

思路:求最近点对。最近距离的一半就是最大半径。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <limits>
 6 using namespace std;
 7 
 8 #define maxn 100010
 9 #define EPS 0.000001
10 int n;
11 double d;//最近距离
12 int cp[maxn];//存中间部分的点的下标
13 struct node{
14     double x, y;
15 }p[maxn];
16 
17 //两点间的欧式距离
18 double pdis(node& p1, node& p2){
19     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
20 }
21 
22 //根据y坐标从小到大给点排序
23 bool cmpy(int a, int b){
24     return p[a].y > p[b].y;
25 }
26 
27 //根据x坐标从小到大给点排序
28 bool cmpx(node p1, node p2){
29     return p1.x < p2.x;
30 }
31 
32 //最近点对的主函数
33 inline void solve(int begin, int end){
34     if(begin == end) return;
35     int mid = (begin+end)/2;
36 
37     solve(begin, mid);
38     solve(mid + 1, end);
39 
40     //找出满足在p[mid]左右d范围内的所有点
41     double midx = p[mid].x;
42     int top = -1;
43     for (int i = begin; i <= end; i++){
44         if(fabs(midx - p[i].x) - d < -EPS)
45             cp[++top] = i;
46     }
47 
48     sort(cp, cp+top+1, cmpy);
49 
50     //向后查点,更新d值
51     for (int i = 0; i <= top; i++){
52         for(int j = i + 1; j <= i + 7 && j <= top; j++){
53             double t = pdis(p[cp[i]],p[cp[j]]);
54             if(t - d < -EPS) d = t;
55         }
56     }
57 }
58 
59 int main()
60 {
61     while(scanf("%d", &n), n){
62         for (int i = 0; i < n; i++){
63             scanf("%lf %lf", &p[i].x, &p[i].y);
64         }
65         sort(p, p+n, cmpx);
66         d = numeric_limits<int>::max();
67         solve(0, n-1);
68         printf("%.2lf\n", d/2.0);
69     }
70     return 0;
71 }

 

posted @ 2016-02-23 14:19  喷水小火龙  阅读(190)  评论(0)    收藏  举报