跳到正文

个人记录

平面上的最接近点对 :我们充分发扬人类智慧!

liubw_约 7 分钟
原文
文章信息与版本

文章信息

发布
2022年4月7日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1
代码
我们充分发扬人类智慧:

将所有点全部绕原点旋转同一个角度,然后按 x 坐标排序

根据数学直觉,在随机旋转后,答案中的两个点在数组中肯定不会离得太远

所以我们只取每个点向后的 5 个点来计算答案

这样速度快得飞起,在 n=1000000 时都可以在 1 s 内卡过

摘自大佬3A17K的一篇博客

但我旋转坐标系搞炸了!

没有关系。

我们还可以充分发扬人类智慧,通过评测机输出情况来多次调试

将节点按照 x∗yx*y 关键字排序,并计算距离时进行 具有面向数据编程性质的 防溢出特判

根据数学直觉,在排序后,答案中的两个点在数组中肯定不会离得太远

为了稳妥起见,我们选择尽可能多的枚举相邻节点,所以我们只取相邻的 25002500 个点来计算

这样就可以非常稳定的暴力 Ac 这道披着计算几何外衣的分治好题了!

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;

template <class T>void read(T &x){
	x=0;
	char c=getchar(),d='0';
	while(c<'0'||c>'9') d=c,c=getchar();
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();
	if(d=='-') x=-x;
}
template <class T>void wt(T x){
	if(x/10) wt(x/10);
	putchar(x%10+'0');
}
template <class T>void enter(T x){
	if(x<0) x=-x,putchar('-');
	wt(x),putchar('\n');
}
template <class T>void space(T x){
	if(x<0) x=-x,putchar('-');
	wt(x),putchar(' ');
}

const int N=2e5+10;

struct node{
	double x,y;
	bool operator < (const node b) const{
		return x*y<b.x*b.y;
	}
	void init(){
		int a,b;
		read(a),read(b);
		x=a,y=b;
	}
}a[N];
double dis(node a,node b){
	if(a.x==b.x) return a.y-b.y>0? a.y-b.y:b.y-a.y;
	if(a.y==b.y) return a.x-b.x>0? a.x-b.x:b.x-a.x;
	#define D(A,B) (A*A-2*A*B+B*B)
	return sqrt(D(a.x,b.x)+D(a.y,b.y));
}

int main(){
	int n;
	read(n);
	for(int i=1;i<=n;i++) a[i].init();
	sort(a+1,a+1+n);
	double ans=0x7ffffffffff;
	int l=2000;
	for(int i=1;i<=n;i++){
		for(int j=i-l+1;j<i;j++){
			int plus=0;
			if(j<1) plus=n;
			double rec=dis(a[i],a[j+plus]);
			if(rec==0){
				space((int)a[i].x),enter((int)a[i].y);
				space((int)a[j].x),enter((int)a[j].y);
				puts("rec==0 ");
			}
			ans=ans<rec? ans:rec;
		}
	}
	printf("%.4lf\n",ans);
	return 0;
}

update (2022.2.19) :

发现学弟们(应该没有学妹吧)学习分治的时候要做到这道题,所以我痛定思痛优化了一下之前非常之劣的算法。

还是 x∗yx*y 排序,特判之后枚举相邻五个即可。

运行时间远低于常规分治。

当然,这是个错误写法。只是数据随机的时候正确率很高罢了。(考场骗分)

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;

template <class T>void read(T &x){
	x=0;
	char c=getchar(),d='0';
	while(c<'0'||c>'9') d=c,c=getchar();
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();
	if(d=='-') x=-x;
}
template <class T>void wt(T x){
	if(x/10) wt(x/10);
	putchar(x%10+'0');
}
template <class T>void enter(T x){
	if(x<0) x=-x,putchar('-');
	wt(x),putchar('\n');
}
template <class T>void space(T x){
	if(x<0) x=-x,putchar('-');
	wt(x),putchar(' ');
}

const int N=2e5+10;

struct node{
	double x,y;
	bool operator < (const node b) const{
		return x*y<b.x*b.y;
	}
	void init(){
		int a,b;
		read(a),read(b);
		x=a,y=b;
	}
}a[N];
double dis(node a,node b){
	if(a.x==b.x) return a.y-b.y>0? a.y-b.y:b.y-a.y;
	if(a.y==b.y) return a.x-b.x>0? a.x-b.x:b.x-a.x;
	#define D(A,B) (A*A-2*A*B+B*B)
	return sqrt(D(a.x,b.x)+D(a.y,b.y));
}

int main(){
	int n;
	read(n);
	for(int i=1;i<=n;i++) a[i].init();
	sort(a+1,a+1+n);
	double ans=0x7ffffffffff;
	if(n<=100){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j) continue;
				double rec=dis(a[i],a[j]);
				ans=ans<rec? ans:rec;
			}
		}

	}
	else{
		int l=5;
		for(int i=l;i<=n;i++){
			for(int j=i-l+1;j<i;j++){
				double rec=dis(a[i],a[j]);
				ans=ans<rec? ans:rec;
			}
		}
	}
	printf("%.4lf\n",ans);
	return 0;
}
原文