【luogu P1257】【luogu P1429】平面上的最接近点对 / 平面最近点对(加强版)

平面上的最接近点对 / 平面最近点对(加强版)

题目链接:luogu P1257 / luogu P1429

题目大意

给你平面上的一堆点,要你找距离最近的两个的距离。

思路

就直接上分治,不会平面分治的可以看这个

代码

#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define INF 2000000000.0

using namespace std;

struct zb {
	double x, y;
}a[200001], tmp[200001];
int n, tn;

double dis(zb x, zb y) {
	return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y));
}

bool cmpx(zb x, zb y) {
	return x.x < y.x;
}

bool cmpy(zb x, zb y) {
	return x.y < y.y;
}

double slove(int l, int r) {
	double re;
	if (l == r) return INF;
	
	int mid = (l + r) >> 1;
	re = min(slove(l, mid), slove(mid + 1, r));
	
	tn = 0;
	for (int i = mid; i >= l && a[mid].x - a[i].x < re; i--)
		tmp[++tn] = a[i];
	for (int i = mid + 1; i <= r && a[i].x - a[mid].x < re; i++)
		tmp[++tn] = a[i];
	
	sort(tmp + 1, tmp + tn + 1, cmpy);
	
	for (int i = 1; i <= tn; i++)
		for (int j = i + 1; j <= tn && tmp[j].y - tmp[i].y < re; j++)
			re = min(re, dis(tmp[i], tmp[j]));
	
	return re;
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%lf %lf", &a[i].x, &a[i].y);
	
	sort(a + 1, a + n + 1, cmpx);
	printf("%.4lf", slove(1, n));
		
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值