海岛Blog

海是无垠的,岛在海中是独立的,有一片自己的天空

HDU1007 ZOJ2107 Quoit Design【二分】

Quoit Design

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


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

问题链接HDU1007 ZOJ2107 Quoit Design

问题简述

  给定n个点的坐标,求其中最近两个点距离的一半。

问题分析

  这个问题基本上就是求最近点的距离,先考虑一个方向,算出一个小的距离来,然后再考虑另外一个方向,用二分搜索来实现。

程序说明:(略)

题记:(略)

参考链接:(略)


AC的C++语言程序如下:
/* HDU1007 ZOJ2107 Quoit Design */

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;

const int N = 1e5;
struct Point {
    double x, y;
} px[N], py[N];

bool cmpx(Point& p1, Point& p2) {return p1.x < p2.x;}
bool cmpy(Point& p1, Point& p2) {return p1.y < p2.y;}

double getDist(Point& p1, Point& p2)
{
    return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}

double distance(int start, int end)
{
    if(end - start == 1)
        return getDist(px[start], px[end]);
    else if(end - start == 2)
        return min(getDist(px[start], px[start + 1]), min(getDist(px[start + 1], px[end]), getDist(px[start], px[end])));
    else {
        int mid = (start + end) / 2;
        double ans = min(distance(start, mid), distance(mid + 1, end));

        int cnt = 0;
        for(int i = start; i <= end; i++)
            if(px[i].x >= px[mid].x - ans && px[i].x <= px[mid].x + ans)
                py[cnt++] = px[i];

        sort(py, py + cnt, cmpy);

        for(int i = 0; i < cnt; i++)
            for(int j = i + 1; j < cnt; j++) {
                if(py[j].y - py[i].y >= ans)
                    break;
                ans = min(ans, getDist(py[i], py[j]));
            }

        return ans;
    }
}

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        for(int i = 0; i < n; i++)
            scanf("%lf%lf", &px[i].x, &px[i].y);

        sort(px, px + n, cmpx);

        printf("%.2f\n", distance(0, n - 1) / 2);
    }

    return 0;
}






阅读更多
所属专栏: ACM题解
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭