HDU-1007

Quoit Design

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


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
 

 

Recommend
JGShining
 
 
题意:求最近点对。
思路:二分
代码:
View Code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100005
#define INF 1999999999

struct tagPoint
{
    double x;
    double y;
}Point[MAX], tmp[MAX];

int cmp1(const void *a, const void *b)
{
    tagPoint *c = (tagPoint *)a;
    tagPoint *d = (tagPoint *)b;
    if (c->x != d->x)
    {
        return c->x > d->x ? 1 : -1;
    }
    return c->y > d->y ? 1 : -1;
}

int cmp2(const void* a,const void* b)
{
    return ((struct tagPoint *)a)->y > ((struct tagPoint *)b)->y ? 1 : -1;
}

double cal_dis1(int i, int j)
{
    return sqrt ( (Point[i].x - Point[j].x) * (Point[i].x - Point[j].x)
        + (Point[i].y - Point[j].y) * (Point[i].y - Point[j].y));
}

double cal_dis2(int i, int j)
{
    return sqrt ( (tmp[i].x - tmp[j].x) * (tmp[i].x - tmp[j].x)
        + (tmp[i].y - tmp[j].y) * (tmp[i].y - tmp[j].y));
}

void find (int l, int r, int mid, double &d)
{
    int i = 0;
    if (r < l)
    {
        return;
    }
    double d1 = d;
    double d2 = d;
    if (l == mid)
    {
        d1 = INF;
    }
    else
    {
        find (l, mid, (l + mid) / 2, d1);
    }
    if (mid + 1 == r)
    {
        d2 = INF;
    }
    else
    {
        find (mid + 1, r, (mid + 1 + r) / 2, d2);
    }
    d = d1 < d2 ? d1 : d2;
    int ll = mid;
    int rr = mid;
    for (i = mid - 1; i >= l; i--)
    {
        if (Point[mid].x - Point[i].x <= d)
        {
            ll = i;
        }
        else
        {
            break;
        }
    }
    for (i = mid + 1; i <= r; i++)
    {
        if (Point[i].x - Point[mid].x <= d)
        {
            rr = i;
        }
        else
        {
            break;
        }
    }
    for (i = ll; i <= mid; i++)
    {
        tmp[i - ll + 1] = Point[i];
    }
    for (i = mid + 1; i <= rr; i++)
    {
        tmp[i -ll + 1] = Point[i];
    }
    qsort (tmp + 1, rr - ll + 1, sizeof (tmp[1]), cmp2);
    double dis = 0;
    for (i = 1; i <= rr - ll + 1; i++)
    {
        int j = i + 1;
        while (j <= rr - ll + 1)
        {
            if (tmp[j].y - tmp[i].y > d)
            {
                break;
            }
            dis = cal_dis2(i, j);
            if (dis < d)
            {
                d = dis;
            }
            j++;
        }
    }
}

int main()
{
    int i = 0;
    int n = 0;
    double d = 0;
    while ( scanf ("%d", &n) != EOF && n )
    {
        for (i = 1; i <= n; i++)
        {
            scanf ("%lf%lf", &Point[i].x, &Point[i].y);
        }
        d = cal_dis1(1, 2);
        qsort (Point + 1, n, sizeof (Point[1]), cmp1);
        find (1, n, (1 + n) / 2, d);
        printf ("%.2lf\n", d / 2);
    }
    return 0;
}

 

posted @ 2012-05-27 18:33  长虹落日  阅读(164)  评论(0编辑  收藏  举报