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;
}

posted on   itdef  阅读(4)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
历史上的今天:
2023-01-02 220. 最长公共子序列问题(挑战程序设计竞赛)
2023-01-02 213. 字典序最小问题 Best Cow Line(挑战程序设计竞赛)
2018-01-02 windows下使用redis c++

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示