J_y_AC_t_k @ 2024-07-11 18:28:50
rt,人都已经要似了qwq
就是归并,复杂度
马蜂良好:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define double long double
inline int read(){
int rt=0; bool kk=0; char g=getchar();
while(g<'0'||g>'9') kk|=(g=='-'),g=getchar();
while(g>='0'&&g<='9') rt=(rt<<3)+(rt<<1)+g-'0',g=getchar();
return (kk?-rt:rt);
}
int n,num;
double ans=1e18;
struct node{double x,y;}p[400005],ls[400005];
inline bool cmp(const node &A,const node &B){return A.x<B.x;}
inline double dis(const node &A,const node &B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}
inline void work(int L,int R)
{
if(L==R) return;
int mid=(L+R)>>1; node now=p[mid];
work(L,mid); work(mid+1,R);
int l=L,r=mid+1; num=0;
while(l<=mid&&r<=R) ls[++num]=(p[l].y<p[r].y?p[l++]:p[r++]);
while(l<=mid) ls[++num]=p[l++];
while(r<=R) ls[++num]=p[r++];
num=0;
for(int i=L;i<=R;i++)
{
p[i]=ls[i-L+1];
if(abs(p[i].x-now.x)<ans) ls[++num]=p[i];
}
int j=1;
for(int i=1;i<num;i++)
{
j=max(i,j);
while(j<num&&ls[j+1].y-ls[i].y<ans)
j++,ans=min(ans,dis(ls[i],ls[j]));
}
}
int main()
{
n=read();
for(int i=1,x,y;i<=n;i++) x=read(),y=read(),p[i]={x,y};
sort(p+1,p+1+n,cmp);
work(1,n);
printf("%.0Lf",ans*ans);
return 0;
}
by MushR @ 2024-08-28 17:54:18
为什么要用 double 啊?
这精度可能会飘吧