跳到正文

剪贴板

剪贴板 ii9ajt84

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#define x first
#define y second
using ll=long long;
const int N=44e4;
using namespace std;
using bbc=pair<int,int>;
int n;
ll D;
bbc p[N];
set<bbc>S;
bbc rev(bbc a)
{
	return bbc(a.y,a.x);
}
ll sqr(int a)
{
	return 1ll*a*a;
}
ll dis(bbc a,bbc b)
{
	return sqr(a.x-b.x)+sqr(a.y-b.y);
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;++i)scanf("%d%d",&p[i].x,&p[i].y);
	D=dis(p[0],p[1]),sort(p,p+n);
	for(int t=0,h=0;t<n;++t)
	{
		bbc o=rev(p[t]);
		while(D&&D<=sqr(p[t].x-p[h].x))S.erase(rev(p[h++]));
		auto it=S.lower_bound(o),k=it;
		for(;k!=S.end()&&sqr(k->x-o.x)<D;++k)
			D=min(D,dis(*k,o));
		if(it!=S.begin())
			for(k=--it;sqr(k->x-o.x)<D;--k)
			{
				D=min(D,dis(*k,o));
				if(k==S.begin())break;
			}
		S.insert(o);
	}
	printf("%lld",D);
	return 0;
}