二分思想,加一点小小的剪枝
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
#define INF 100000
struct Point{
double x,y;
}p[100005],st[100005];
int n;
double min(double x,double y){return x<y?x:y;}
double dist(Point sp,Point op){
return sqrt((sp.x-op.x)*(sp.x-op.x)+(sp.y-op.y)*(sp.y-op.y));
}
bool cmp(const Point &a,const Point &b){
return a.x<b.x;
}
bool cmp1(const Point &a,const Point &b){
return a.y<b.y;
}
void update(double &d,int m,int l,int r){
int i=m-1,j=m+1,tot=0;
st[++tot]=p[m];
while(p[m].x-p[i].x<d&&i>=l){st[++tot]=p[i];i--;}
while(p[j].x-p[m].x<d&&j<=r){st[++tot]=p[j];j++;}
/*i++,j--;
while(i<=j){
st[++tot]=p[i];
i++;
}*/
sort(st+1,st+tot+1,cmp1);
for(i=1;i<tot;i++)
for(j=i+1;j<=tot;j++){
if(st[j].y-st[i].y>=d)break;
d=min(d,dist(st[i],st[j]));
}
}
double work(int l,int r){
if(l==r)return INF;
if(r-l==1)return dist(p[l],p[r]);
double dl,dr,d;
int m=l+(r-l)/2;
dl=work(l,m);
dr=work(m,r);
d=min(dl,dr);
update(d,m,l,r);
return d;
}
int main(){
freopen("nearest.in","r",stdin);
freopen("nearest.out","w",stdout);
scanf("%d",&n);
//printf("%d\n",n);
int i;
for(i=1;i<=n;i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
sort(p+1,p+n+1,cmp);
/*for(i=1;i<=n;i++)
printf("%.2lf %.2lf\n",p[i].x,p[i].y);*/
double ans=work(1,n);
printf("%.2lf\n",ans);
return 0;
}