Pagini recente » Cod sursa (job #1983020) | Cod sursa (job #2512189) | Cod sursa (job #1615732) | Cod sursa (job #1028821) | Cod sursa (job #2067678)
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
ifstream f("cmap.in");
ofstream g("cmap.out");
#define Nmax 100010
#define Inf 1<<30
struct punct
{
int x, y;
}v[Nmax],w[Nmax];
int n;
bool cmp (punct a, punct b)
{
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
bool cmpy(punct a, punct b)
{
return a.y < b.y;
}
double dist ( punct a, punct b)
{
double difx = a.x - b.x;
double dify = a.y - b.y;
return sqrt ( difx * difx + dify * dify ) ;
}
double divide ( int st, int dr)
{
if( dr - st == 1)
return dist( v[st], v[dr] );
else
if ( dr-st == 2 )
{
double D, Dmin=Inf;
int i, j;
for(i = st; i < dr; i++)
for(j = i + 1 ; j <= dr; j++)
{
D = dist( v[i] , v[j] );
if(D < Dmin)
Dmin = D;
}
return Dmin;
}
int m=( st + dr ) / 2;
double S1 = divide ( st, m ) ;
double S2 = divide ( m+1, dr ) ;
int d = v[m].x;
double S = S1;
if(S2 < S1)
S = S2;
int i,j = m+1 ,k=0;
double delta = ceil(sqrt(d));
for( i = st; i <= dr; i++)
{
if( abs( v[i].x - d ) <= delta )
{
w[k].x = v[i].x;
w[k].y = v[i].y;
k++;
}
}
sort(w,w+k,cmpy);
double D, Dmin=Inf;
for(i = 0; i < k ; i++)
for(j = i + 1 ; j <= (i+7) && j < k; j++)
{
D = dist(w[i],w[j]);
if(D < Dmin)
Dmin=D;
}
if(Dmin > S)
Dmin = S;
return Dmin;
}
int main()
{
int n;
f >> n;
for ( int i = 1; i <= n ;i++)
f >> v[i].x >> v[i].y;
sort(v+1,v+n+1,cmp);
g << fixed << setprecision(6) << divide(1,n);
return 0;
}