603 平面最近点对
// 603 平面最近点对.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
http://oj.daimayuan.top/course/22/problem/979
给定平面上 n个点,你需要求出其中最近的一对点的欧几里得距离。
输入格式
第一行一个整数 n。
接下来 n行,每行两个数 xi,yi, 表示一个点的坐标。
输出格式
输出一行一个数表示答案,相对误差或绝对误差在 10−6内即为正确。
样例输入
3
1.0 2.0
1.0 3.0
1.0 5.0
样例输出
1.00000000
数据规模
对于 100%的数据,保证 1≤n≤2×105,0≤xi,yi≤109。
*/
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 200010;
struct Node {
double x, y;
bool operator < (const Node& a) const {
return x < a.x;
}
}a[N],c[N];
int n;
double solve(int l,int r){
if (l == r) return 1e10;
int m = (l + r) >> 1; int cnt = 0;
double d = min(solve(l, m), solve(m + 1, r));
for (int i = l; i <= r; i++) {
if (a[i + 1].x - a[m].x < d) {
c[++cnt].y = a[i].x; c[cnt].x = a[i].y;
}
}
sort(c + 1, c + cnt + 1);
for (int i = 1; i < cnt; i++) {
double xlen = c[i].x - c[i + 1].x;
double ylen = c[i].y - c[i + 1].y;
d = min(d, sqrt(xlen * xlen + ylen * ylen));
}
return d;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].x >> a[i].y;
}
sort(a+1, a + n+1);
printf("%.10f\n",solve(1,n));
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力

