2. 套圈(分治)

2023-12-17 13:50:43

题目

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.

你在游乐场玩过扔圆环游戏吗?扔圆环是一种游戏,玩家需要将平面圆环扔向一些玩具,所有被圆环围住的玩具都会得到奖励。在虚拟游戏场地中,每个玩具的位置是固定的,而圆环被精心设计,只能一次性围住一个玩具。另一方面,为了使游戏看起来更吸引人,圆环被设计成具有最大的半径。给定场地的配置,你需要找出这样一个圆环的半径。假设所有玩具都是平面上的点。如果一个点与圆环的中心之间的距离严格小于圆环的半径,那么该点就被圆环围住。如果两个玩具放在同一个位置,则认为圆环的半径为0。

输入 输入包括多个测试用例。对于每个案例,第一行包含一个整数N(2 <= N <= 100,000),表示场地中的玩具总数。接下来是N行,每行包含一个(x, y)对,表示一个玩具的坐标。当N = 0时,输入终止。

输出 对于每个测试案例,以一行输出所需的虚拟游戏场地经理所需的圆环半径,精确到小数点后两位。


思路参考:

https://blog.csdn.net/weixin_43787043/article/details/104525990?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170278315416800226539188%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170278315416800226539188&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-104525990-null-null.142^v96^pc_search_result_base4&utm_term=2.%20%E5%A5%97%E5%9C%88&spm=1018.2226.3001.4187icon-default.png?t=N7T8https://blog.csdn.net/weixin_43787043/article/details/104525990?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170278315416800226539188%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170278315416800226539188&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-104525990-null-null.142^v96^pc_search_result_base4&utm_term=2.%20%E5%A5%97%E5%9C%88&spm=1018.2226.3001.4187


代码

#include <iostream>  
#include <vector>  
#include <algorithm>  
#include <cmath>  
#include <iomanip>  

using namespace std;

// 定义一个结构体来存储玩具的坐标  
struct Point {
    double x, y;
};

// 按照x坐标排序的比较函数  
bool compareX(const Point& a, const Point& b) {
    return a.x < b.x;
}

// 计算两点之间的距离  
double distance(const Point& a, const Point& b) {
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

// 用于递归的分治算法  
double closestPairRecursive(vector<Point>& pointsX, size_t left, size_t right) {
    if (right - left <= 3) {
        // 当点的数量少时直接计算  
        double minDist = numeric_limits<double>::max();
        for (size_t i = left; i < right; ++i) {
            for (size_t j = i + 1; j < right; ++j) {
                minDist = min(minDist, distance(pointsX[i], pointsX[j]));
            }
        }
        return minDist;
    }

    // 找到中间点  
    size_t mid = left + (right - left) / 2;
    double midX = pointsX[mid].x;

    // 分治递归  
    double d1 = closestPairRecursive(pointsX, left, mid);
    double d2 = closestPairRecursive(pointsX, mid, right);
    double d = min(d1, d2);

    // 归并排序的步骤,按y坐标排序  
    vector<Point> strip;
    for (size_t i = left; i < right; ++i) {
        if (fabs(pointsX[i].x - midX) < d) {
            strip.push_back(pointsX[i]);
        }
    }
    sort(strip.begin(), strip.end(), [](const Point& a, const Point& b) {
        return a.y < b.y;
        });

    // 在条带中找到最小距离  
    for (size_t i = 0; i < strip.size(); ++i) {
        for (size_t j = i + 1; j < strip.size() && (strip[j].y - strip[i].y) < d; ++j) {
            d = min(d, distance(strip[i], strip[j]));
        }
    }

    return d;
}

// 最近点对算法的主函数  
double closestPair(vector<Point>& points) {
    vector<Point> pointsX(points.begin(), points.end());

    // 根据x坐标排序  
    sort(pointsX.begin(), pointsX.end(), compareX);

    // 使用分治算法  
    return closestPairRecursive(pointsX, 0, points.size());
}

int main() {
    int N;
    while (cin >> N && N != 0) {
        vector<Point> toys(N);
        for (int i = 0; i < N; ++i) {
            cin >> toys[i].x >> toys[i].y;
        }

        double minDist = closestPair(toys);
        double radius = minDist / 2.0;

        cout << fixed << setprecision(2) << radius << endl;
    }
    return 0;
}

文章来源:https://blog.csdn.net/m0_74200772/article/details/135043337
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。