因为这道题 ,所以时间复杂度 的算法可以通过。直接枚举两个点,然后计算它们的距离即可。两点间的距离是 。
管理员在这篇帖子中 hack 了所有的题解,但这篇题解能够通过 hack 数据,而且 的初始值足够大,不会因初始值小被卡掉。
#include <bits/stdc++.h>
using namespace std;
long long x[10010], y[10010], n;
long long ans = 1.5 * 1e9 * 1.5 * 1e9; // 考虑到两点间的最远距离是 sqrt(2)*1e9
const long double eps = 1e-8;
long double Nsqrt(const long double &x) { // 计算平方根
if (x == 0) return 0;
long double a = 0, b = x;
while (((a > b) ? (a - b) : (b - a)) > eps) {
a = b;
b = (a + x / a) / 2;
}
return b;
}
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld%lld", &x[i], &y[i]);
for (int j = 1; j < i; j++) {
ans = min(ans, (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); // 欧几里得距离的平方
}
}
printf("%.4Lf\n", Nsqrt((long double)ans)); // 开平方根,计算距离
return 0;
}