【POJ3714】Raid

本文深入探讨了平面最近点对问题的经典算法实现,通过详细分析和代码示例,展示了如何在平面上找到两个最接近的点对。该算法适用于各种需要计算点间距离的应用场景。

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

题面

分析

平面最近点对板子题。虽然我是看了lyd大佬的标程才会的......所以代码和标程.......

代码

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define il inline
#define re register
#define tie0 cin.tie(0),cout.tie(0)
#define fastio ios::sync_with_stdio(false)
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
typedef long long ll;
typedef long double ld;

template <typename T> inline void read(T &x) {
    T f = 1; x = 0; char c;
    for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1;
    for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
    x *= f;
}

const double INF = 0x3f3f3f3f;
const double eps = 1e-7;

struct rd {
    int x, y;
    bool army;
    bool operator < (const rd &k) const { return x < k.x; }
} ai[200005];

int n;

double dis(rd a, rd b) {
    if (a.army == b.army) return INF;
    return sqrt((ld)(a.x - b.x) * (a.x - b.x) + (ld)(a.y - b.y) * (a.y - b.y));
}

double raid(int l, int r) {
    if (l == r) return INF;
    if (r == l + 1) return dis(ai[l], ai[r]);
    int mid = l + r >> 1;
    double ans = min(raid(l, mid), raid(mid, r));
    for (int i = mid - 1; i >= l; --i) {
        if (ai[mid].x - ai[i].x + eps > ans) break;
        for (int j = mid + 1; j <= r; ++j) {
            if (ai[j].x - ai[i].x + eps > ans) break;
            ans = min(ans, dis(ai[i], ai[j]));
        }
    }
    return ans;
}

void solve() {
    read(n);
    for (int i = 1; i <= n; ++i) {
        read(ai[i].x), read(ai[i].y);
        ai[i].army = 0;
    }
    for (int i = 1; i <= n; ++i) {
        read(ai[i+n].x), read(ai[i+n].y);
        ai[i+n].army = 1;
    }
    sort(ai + 1, ai + 1 + n * 2);
    printf("%.3f\n", raid(1, 2 * n));
}

int main() {
    int t;
    read(t);
    while (t--) solve();
    return 0;
}

转载于:https://www.cnblogs.com/hlw1/p/11319371.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值