求优化/kel
查看原帖
求优化/kel
766517
Loop1st楼主2024/5/24 12:14

感觉写法对了呀/kk

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
typedef long long ll;
typedef pair<int, int> pii;
template <class T>
T read() {
	char c = getchar();
	bool flag = (c == '-');
	T num = 0;
	while (c < '0' || c > '9') c = getchar(), flag |= (c == '-');
	while (c >= '0' && c <= '9') num = (num << 1) + (num << 3) + (c ^ 48), c = getchar();
	return flag ? -num : num;
}
struct Node {
	ll x, y;
	bool operator < (const Node &A) const {
		return x < A.x;
	}
} a[N], c[N];
int n;
inline ll getDis(Node a, Node b) {
	auto [x1, y1] = a; auto [x2, y2] = b;
	return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
inline ll solve(int l, int r) {
	if (l == r) return 1e17;
	int mid = (l + r) >> 1;
	ll res = min(solve(l, mid), solve(mid + 1, r));
	int tot = 0;
	for (int i = l; i <= r; i++) {
		if (abs(a[i].x - a[mid].x) < res) {
			c[++tot] = {a[i].y, a[i].x};
		}
	}
	sort(c + 1, c + tot + 1);
	for (int i = 1; i <= tot; i++) {
		for (int j = i + 1; j <= tot && c[j].x - c[i].x < res; j++) {
			res = min(res, getDis(c[i], c[j]));
		}
	}
	return res;
}
int main() {
	n = read<int>();
	for (int i = 1; i <= n; i++) a[i].x = read<ll>(), a[i].y = read<ll>();
	sort(a + 1, a + n + 1);
	printf("%lld\n", solve(1, n));
	return 0;
}
2024/5/24 12:14
加载中...