Pagini recente » Cod sursa (job #1589544) | Cod sursa (job #286749) | Cod sursa (job #1751543) | Cod sursa (job #1704795) | Cod sursa (job #1035833)
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define INF 2000000000
#define NMAX 100003
struct punct
{
int x,y;
};
int n,na,poz[NMAX];
punct v[NMAX],a[NMAX];
inline bool cmpx(const punct &a,const punct &b)
{return a.x<b.x;}
inline bool cmpy(const punct &a,const punct &b)
{return a.y<b.y;}
inline double dist(punct i,punct j)
{
return sqrt((i.x-j.x)*(i.x-j.x) + (i.y-j.y)*(i.y-j.y));
}
inline double caz3(int l,int r)
{
double sol=INF;
if(r-l+1>=2)
sol=dist(v[poz[l]],v[poz[l+1]]);
if(r-l+1==3)
{
sol=min(sol,dist(v[poz[l]],v[poz[l+2]]));
sol=min(sol,dist(v[poz[l+1]],v[poz[l+2]]));
}
return sol;
}
double rez(int l,int r)
{
double sol=INF;
if(r-l+1<=3)
{
sol=caz3(l,r);
return sol;
}
int med=(l+r)>>1,i,j,p=1;
sol=min(rez(l,med) , rez(med+1,r));
na=0;
for(i=l;i<=r;++i)
if(abs(v[poz[med]].x-v[poz[i]].x)<=sol)
a[++na]=v[poz[i]];
sort(a+1,a+1+na,cmpy); //
for(i=2;i<=na;++i)
{
while(a[i].y-a[p].y>sol)
++p;
for(j=p;j<i;++j)
sol=min(sol,dist(a[i],a[j]));
}
return sol;
}
int main()
{
freopen("cmap.in","r",stdin);
freopen("cmap.out","w",stdout);
int i;
double sol;
scanf("%d",&n);
for(i=1;i<=n;++i)
{
scanf("%d%d",&v[i].x,&v[i].y);
poz[i]=i;
}
sort(v+1,v+1+n,cmpx); //
sol=rez(1,n);
printf("%.6lf\n",sol);
return 0;
}