分治,两个点TLE

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

把f函数里面的排序去了就能过了 ```cpp #include <bits/stdc++.h> using namespace std; const long long INF = 1e15; int n; struct Point { long long x, y; Point(long long x = 0, long long y = 0): x(x), y(y) {} } P[400005]; bool cmp1(Point A, Point B) { if (A.x != B.x) return A.x < B.x; return A.y < B.y; } int cnt; Point tmp[400005]; bool cmp2(Point A, Point B) { return A.y < B.y; } long long f(int l, int r) { if (l == r) return INF; int mid = l + r >> 1; long long minn1 = f(l, mid); long long minn2 = f(mid + 1, r); long long minD = min(minn1, minn2); cnt = 0; for (int i = l; i <= r; i++) { if (llabs(P[mid].x - P[i].x) < minD) { tmp[++cnt] = P[i]; } } //sort(tmp + 1, tmp + cnt + 1, cmp2); for (int i = 1; i <= cnt; i++) { for (int j = i + 1; j <= cnt && tmp[j].y - tmp[i].y < minD; j++) { minD = min(minD, (tmp[i].x - tmp[j].x) * (tmp[i].x - tmp[j].x) + (tmp[i].y - tmp[j].y) * (tmp[i].y - tmp[j].y)); } } return minD; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld %lld", &P[i].x, &P[i].y); } sort(P + 1, P + n + 1, cmp1); printf("%.4lf\n", (double)(sqrt(f(1, n)))); return 0; } ```
by ximengl @ 2024-08-22 12:16:18


|