HDU 3124 (二分+扫描线 最近圆对)

本文深入探讨了如何通过算法计算多个不相交圆盘中最近一对圆盘之间的距离,详细介绍了输入输出规范、约束条件,并提供了一段C++代码实现。通过二分搜索和扫描线技术,有效地解决了复杂场景下的近距离检测问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Moonmist

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1837    Accepted Submission(s): 618


Problem Description
An Unidentified Flying Object (Commonly abbreviated as UFO) is the popular term for any aerial phenomenon whose cause cannot be easily or immediately identified. We always believe UFO is the vehicle of aliens. But there is an interrogation about why UFO always likes a circular saucer? There must be a reason. Actually our scientists are developing a new traffic system “Moonmist”. It is distinguished from the traditional traffic. We use circular saucers in this new traffic system and the saucers moves extremely fast. When our scientists did their test, they found that traffic accident was too hard to be avoided because of the high speed of the advanced saucer. They need us to develop a system that can tell them the nearest saucer. The distance between two saucers is defined as the shortest distance between any two points in different saucers.
 

Input
The first line consists of an integer T, indicating the number of test cases.
The first line of each case consists of an integer N, indicating the number of saucers. Each saucer is represented on a single line, consisting of three integers X, Y, R, indicating the coordinate and the radius. You can assume that the distance between any two saucers will never be zero.
 

Output
For each test case, please output a floating number with six fractional numbers, indicating the shortest distance.
Constraints
0 < T <= 10
2 <= N <= 50000
0 <= X, Y, R <= 100000
 

Sample Input
  
  
1 2 0 0 1 10 10 1
 

Sample Output
  
  
12.142136
 
题意是从n个不相交的圆里找到最近的一对圆的距离.
首先二分结果,然后更新每个圆的左右端点,扫描线在从左往右的过程中,如果扫到圆的左端点,在加入之前判断是不是和上下相邻的圆相交,如果扫到圆的右端点,在删除前也判断下是不是和上下相邻的圆相交.每次发现相交情况及时的return.
复杂度O(n*lgn*lgm),m是自己设的常数,只要比100000*sqrt (2)都可以.

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <iostream>
using namespace std;
#define maxn 111111
#define pb push_back
#define mp make_pair
#define eps 1e-8

double x[maxn], y[maxn], r[maxn];
double rr[maxn];
int n;
vector <pair<double, int> > a;
set <pair<double, int> > gg;

bool xiangjiao (int i, int j) { //判断圆i和圆j是不是相交
    double xx = x[i]-x[j], yy = y[i]-y[j];
    return sqrt (xx*xx+yy*yy) <= rr[i]+rr[j];
}

void stop () {
    while (1) ;
}

bool judge () {
    sort (a.begin (), a.end ());
    for (int i = 0; i < a.size (); i++) {
        int id = a[i].second;
        if (id <= n) { //圆的左边界
            set <pair <double, int> >::iterator it = gg.lower_bound (mp (y[id], id));
            if (it != gg.end ()) {
                if (xiangjiao (id, it->second))
                    return 1;
            }
            if (it != gg.begin ()) {
                if (xiangjiao (id, (--it)->second))
                    return 1;
            }
            gg.insert (mp (y[id], id));
        }
        else {  //圆的右边界
            id -= n;
            set <pair <double, int> >::iterator it = gg.upper_bound (mp (y[id], id));
            if (it != gg.end ()) {
                if (xiangjiao (id, it->second))
                    return 1;
            }
            if (it != gg.begin ()) {
                --it;
                if (it != gg.begin () && xiangjiao (id, (--it)->second))
                    return 1;
            }
            gg.erase (mp (y[id], id));
        }
    }
    return 0;
}

int main () {
    //freopen ("in", "r", stdin);
    int t;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf ("%lf%lf%lf", &x[i], &y[i], &r[i]);
        }
        double L = 0.0, R = 1000000;
        while (R-L > eps) { //二分距离
            double mid = (L+R)/2.0;
            a.clear (); gg.clear ();
            for (int i = 1; i <= n; i++) {
                rr[i] = r[i] + mid/2.0;
                a.pb (mp (x[i]-r[i]-mid/2.0, i));
                a.pb (mp (x[i]+r[i]+mid/2.0, i+n));
            }
            if (judge ()) //存在交点
                R = mid;
            else {        //不存在交点
                L = mid;
            }
        }
        printf ("%.6f\n", (L+R)/2.0);
    }
    return 0;
}


### 解决PyCharm无法加载Conda虚拟环境的方法 #### 配置设置 为了使 PyCharm 能够成功识别并使用 Conda 创建的虚拟环境,需确保 Anaconda 的路径已正确添加至系统的环境变量中[^1]。这一步骤至关重要,因为只有当 Python 解释器及其关联工具被加入 PATH 后,IDE 才能顺利找到它们。 对于 Windows 用户而言,在安装 Anaconda 时,默认情况下会询问是否将它添加到系统路径里;如果当时选择了否,则现在应该手动完成此操作。具体做法是在“高级系统设置”的“环境变量”选项内编辑 `Path` 变量,追加 Anaconda 安装目录下的 Scripts 文件夹位置。 另外,建议每次新建项目前都通过命令行先激活目标 conda env: ```bash conda activate myenvname ``` 接着再启动 IDE 进入工作区,这样有助于减少兼容性方面的问题发生概率。 #### 常见错误及修复方法 ##### 错误一:未发现任何解释器 症状表现为打开 PyCharm 新建工程向导页面找不到由 Conda 构建出来的 interpreter 列表项。此时应前往 Preferences/Settings -> Project:...->Python Interpreter 下方点击齿轮图标选择 Add...按钮来指定自定义的位置。按照提示浏览定位到对应版本 python.exe 的绝对地址即可解决问题。 ##### 错误二:权限不足导致 DLL 加载失败 有时即使指定了正确的解释器路径,仍可能遇到由于缺乏适当的操作系统级许可而引发的功能缺失现象。特别是涉及到调用某些特定类型的动态链接库 (Dynamic Link Library, .dll) 时尤为明显。因此拥有管理员身份执行相关动作显得尤为重要——无论是从终端还是图形界面触发创建新 venv 流程均如此处理能够有效规避此类隐患。 ##### 错误三:网络连接异常引起依赖下载超时 部分开发者反馈过因网速慢或者其他因素造成 pip install 操作中途断开进而影响整个项目的初始化进度条卡住的情况。对此可尝试调整镜像源加速获取速度或是离线模式预先准备好所需资源包后再继续后续步骤。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值