HDU1007--Quoit Design(平面最近点对)

Posted Pic

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1007--Quoit Design(平面最近点对)相关的知识,希望对你有一定的参考价值。

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

Source

ZJCPC2004

Recommend

JGShining

大意:

平面中有n个点,求要使一个固定半径的圆一次只能包围一个点的最大半径

即为求点集中的最近点对

思路:

采用了算法导论33.4节中介绍的分治法求平面最近点对,时间复杂度为:O(nlogn)

代码:

//平面最近点对,使用分治法
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
const double eps = 1e-6;
const int MAXN = 100010;
const double INF = 1e20;
struct Point
{
    double x, y;
};
double dist(Point a, Point b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
Point p[MAXN];
Point tmpt[MAXN];
bool cmpxy(Point a, Point b)//排序时的比较函数
{
    if (a.x != b.x)return a.x < b.x;
    else return a.y < b.y;
}
bool cmpy(Point a, Point b)//按照y值排序
{
    return a.y < b.y;
}
double Closest_Pair(int left, int right)
{
    double d = INF;

    if (left == right)return d;
    if (left + 1 == right)
        return dist(p[left], p[right]);//递归边界

    int mid = (left + right) / 2;

    double d1 = Closest_Pair(left, mid);//分治求两个点集合的最近点对
    double d2 = Closest_Pair(mid + 1, right);

    d = min(d1, d2);
    int k = 0;
    for (int i = left; i <= right; i++)
    {
        if (fabs(p[mid].x - p[i].x) <= d)
            tmpt[k++] = p[i];
    }        //tmpt为与中线距离小于等于d的点的集合
    sort(tmpt, tmpt + k, cmpy);
    for (int i = 0; i < k; i++)
    {
        for (int j = i + 1; j < k && tmpt[j].y - tmpt[i].y < d; j++)
        {
            d = min(d, dist(tmpt[i], tmpt[j]));
        }
    }//合并分治结果
    return d;
}
int main()
{
    int n;
    while (scanf("%d", &n) == 1 && n)
    {
        for (int i = 0; i < n; i++)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        sort(p, p + n, cmpxy);//对p进行预排序
        printf("%.2lf\n", Closest_Pair(0, n - 1) / 2);
    }
    return 0;
}

以上是关于HDU1007--Quoit Design(平面最近点对)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1007 Quoit Design

hdu1007 Quoit Design

HDU - 1007 Quoit Design

HDU1007:Quoit Design——题解

分治算法应用-最近点对的最小距离-hdu 1007 Quoit Design

HDU 1007 [Quoit Design] 分治