一只眼睛看世界

对应HDU题目:点击打开链接

Quoit DesignTime Limit: 10000/5000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35028Accepted Submission(s): 9150

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

20 01 121 11 13-1.5 00 00 1.50

Sample Output

0.710.000.75

题意:很裸,就是求平面里的最近点对。

思路:可采用分治的思想,把所有点按x递增排序。n个点的最近点对要么在左边的n/2个点中,要么在右边的n/2个点中。要么一个点在左边,一个点在右边组成最近点对。

难点在合并,也就是解决一个点在左边,一个点在右边组成最近点对这种情况。在mid左边跟右边的点要组成最近点对,设中间点的x坐标为x0,那符合要求的点的x坐标应该满足fabs(x – x0) <= 已求得的最小值min_d。那就可以把符合要求的点按y坐标递增排序。然后验证每个符合要求的点跟其他符合要求的点是否组成比min_d更小的距离。是就更新之。这里有个优化就是如果y值之差大于min_d,就可以跳出循环(y是按递增排序的)还有就是其实只需要计算每个点后面的7个点就可以了,,《算法导论》计算几何那就有证明。。。

最后,用qsort超时了,改用sort就可以过。测试了下,sort的确比qsort快那么几倍,至少在windows中是这样。。。

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <iostream>#include <algorithm>#define min(x, y) (x) < (y) ? (x) : (y)#define INF (1<<30)#define N 100100using namespace std;int n;typedef struct{double x, y;}Point;Point p[N];int a[N];double Cal(const Point *p1, const Point *p2){return sqrt((p1->x – p2->x) * (p1->x – p2->x) + (p1->y – p2->y) * (p1->y – p2->y));}bool cmpx(Point p1, Point p2){return p1.x < p2.x;}bool cmpy(int a, int b){return p[a].y < p[b].y;}double Solve(int left, int right){int i, j, cnt = 0;double d, d1, d2, min_d = INF;if(right – left < 3){for(i=left; i<=right; i++)for(j=i+1; j<=right; j++){d = Cal(p + i, p + j);min_d = min(d, min_d);}return min_d;}int mid = left + (right – left) / 2;d1 = Solve(left, mid);d2 = Solve(mid + 1, right);min_d = min(d1, d2);for(i=left; i<=right; i++)if(fabs(p[i].x – p[mid].x) <= min_d){a[cnt++] = i;}sort(a, a + cnt, cmpy);for(i=0; i<cnt; i++){for(j=i+1; j<cnt && j < 8; j++){if(p[a[j]].y – p[a[i]].y >= min_d) break;d = Cal(p + a[i], p + a[j]);min_d = min(d, min_d);}}return min_d;}int main(){//freopen("in.txt", "r", stdin);while(scanf("%d", &n), n){int i;for(i=0; i<n; i++){scanf("%lf%lf", &p[i].x, &p[i].y);}sort(p, p + n, cmpx);double d = Solve(0, n – 1);printf("%.2lf\n", d / 2);}return 0;}

享受每一刻的感觉,欣赏每一处的风景,这就是人生。

一只眼睛看世界

相关文章:

你感兴趣的文章:

标签云: