22pts TLE 正常分治思路

P7883 平面最近点对(加强加强版)

_Ventus_ @ 2023-10-03 21:59:30

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 400010;
int n, b[N];
struct node { int x, y; } a[N];
bool cmp(node a, node b) { return a.x == b.x ? a.y < b.y : a.x < b.x; }
bool cmp1(int x, int y) { return a[x].y < a[y].y; }
inline ll dis(int i, int j) { return 1ll * (a[i].x - a[j].x) * (a[i].x - a[j].x) + 1ll * (a[i].y - a[j].y) * (a[i].y - a[j].y); }
ll solve(int l, int r) {
    ll d = 0x7f7f7f7f;
    if (l == r) return d;
    if (l + 1 == r) return dis(l, r);
    int mid = l + r >> 1;
    ll d1 = solve(l, mid), d2 = solve(mid + 1, r);
    d = min(d1, d2);
    int k = 0;
    for (int i = l; i <= r; i++)
        if (abs(a[mid].x - a[i].x) < d) b[++k] = i;
    sort(b + 1, b + k + 1, cmp1);
    for (int i = 1; i < k; i++)
        for (int j = i + 1; j <= k && a[b[j]].y - a[b[i]].y < d; j++) d = min(d, dis(b[i], b[j]));
    return d;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y;
    sort(a + 1, a + n + 1, cmp);
    cout << solve(1, n) << '\n';
    return 0;
}

by george0929 @ 2023-11-16 20:55:34

21和24行 <d 应该为 <sqrt(d)

因为您代码的 d 在这里表示的是最近点对距离的平方


by george0929 @ 2023-11-16 20:56:18

(我就是这么挂的 QwQ )

@Ventus


by _Ventus_ @ 2023-11-18 14:02:42

@george0929 谢谢,已过


by Field_Mouse @ 2023-11-25 23:17:53

谢谢,我也这么挂了


by __ryp__ @ 2024-02-06 12:38:56

@Ventus 跪谢!!Orz


by Neflibatas @ 2024-03-21 23:38:53

@george0929 感谢


by 123asdf123 @ 2024-05-24 11:21:53

orz


by Y_QWQ_Y @ 2024-11-23 08:08:33

orz


by 日奈 @ 2024-12-03 22:27:58

orz


|