HDU OJ 1007 Quoit Design

问题描述

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.

输入

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.

Sample Input
2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0

输出

For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.

0.71
0.00
0.75

解题思路

  • 给定二维平面内一些数量的点,求出最短的两个点之间的距离的一半。
    经典的分治算法。
  • 首先将这些点对按照x大小排序,取这些点对x的中间值 x m i d x_{mid} xmid把这些点分为左右两个部分分别求左右两个部分,并分别求出两个部分点对的最小值 d m i n 1 d_{min1} dmin1 d m i n 2 d_{min2} dmin2
  • 显然,最小值还可能是左右两边各一个点的情况。显然这些疑似点的x必须满足 ∣ x − x m i d ∣ < m i n ( d m i n 1 , d m i n 2 ) |x-x_{mid}| <min(d_{min1},d_{min2}) xxmid<min(dmin1,dmin2),令 m i n ( d m i n 1 , d m i n 2 ) = min(d_{min1},d_{min2})= min(dmin1,dmin2)=d。
  • 然后把符合条件的点按照y的大小再次排序,从y最小的点遍历排好序的数组。如果计算后面每一个点和它的距离大小,那么复杂度是 O n 2 O_{n^{2}} On2还不如直接暴力破解。我们不妨以第一个点为例,因为y是递增序,因此它可能出现的区域是红色直线部分,显然接下来的点和它的距离要想小于d,只能出现在高为d,宽为2d(本来x就限定在了这个区域)的区域。
    在这里插入图片描述
  • 如果该点在左边的红色线段上,由于左边区域 d m i n 1 ≥ d d_{min1} \ge d dmin1d,根据鸽笼原理,最多左边只有4个比该点的y大且不超过的点。右边同样在 d m i n 2 ≥ d d_{min2}\ge d dmin2d,根据鸽笼原理在右边区域也最多只有4个点满足条件。所以比该点的y大且不超过d的点最多只有8个(实际上可以进一步证明只有6个)。
  • 所以复杂度最多只有8n,也就是O(n),最后复杂度为O(nlogn)。

C++代码

#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <stdio.h>
#include <limits.h>
using namespace std;

double MAX = __DBL_MAX__;

struct Point{
    double x, y;
    int id;
} randByX[100005], randByY[100005];

bool compX(Point a, Point b){ // 按照x的大小来比较
    return a.x < b.x;
}

bool compY(Point a, Point b){ // 按照y的大小来比较
    return a.y < b.y;
}

double getMin(double a, double b){
    return a < b? a: b;
}

double getDis(Point a, Point b){ // 计算两点之间的距离
    return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}

double solve(int left, int right){
    int mid, cur = 0;
    double minDis;
    if(left == right){
        return MAX;
    }
    mid = (left + right) / 2;
    minDis = getMin(solve(left, mid), solve(mid + 1, right));
    for(int i = mid; i >= left && randByX[mid].x - randByX[i].x < minDis; i--){ // 找出带状区域所有点
        randByY[cur++] = randByX[i];
    }
    for(int i = mid + 1; i < right && randByX[i].x - randByX[mid].x < minDis; i++){
        randByY[cur++] = randByX[i];
    }
    sort(randByY, randByY + cur, compY); // 按y排序
    for(int i = 0; i < cur; i++){ // 进一步确定左右两个区域内是否有更小的距离
        for(int j = i + 1; j < cur && randByY[j].y - randByY[i].y < minDis; j++){
            if(minDis > getDis(randByY[i], randByY[j])){
                minDis = getDis(randByY[i], randByY[j]);
            }
        }
    }
    return minDis;
}

int main(){
    int numPoints;
    while(cin >> numPoints && numPoints){
        
        for(int i = 0; i < numPoints; i++){
            randByX[i].id = i + 1;
            scanf("%lf%lf", &randByX[i].x, &randByX[i].y); // 用cin会超时
        }
        sort(randByX, randByX + numPoints, compX);
        double dis = solve(0, numPoints) / 2.0;
        cout << fixed << setprecision(2) << dis << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值