Cod sursa(job #1674071)

Utilizator CrystyAngelDinu Cristian CrystyAngel Data 4 aprilie 2016 12:45:55
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct nod
{
    long long x,y;
};
int n;
inline bool compare(nod a,nod b)
{
    if(a.x!=b.x)
        return a.x<b.x;
    return a.y<b.y;
}
nod v[100010];
double dist(int s,int d)
{
    if(d-s>3)
    {
        double mn=min(dist(s,(s+d)/2),dist((s+d)/2,d));
        int k=0,t[50],i;
        double c=(v[(s+d)/2].x+v[(s+d)/2-1].x)/2;
        i=(s+d)/2;
        while(c-v[i].x<=mn && i>-1)
            t[k++]=i--;
        for(i=0;i<(k>>1);++i)
            swap(t[i],t[k-i-1]);
        i=(s+d)/2+1;
        while(v[i].x-c<=mn && i<n)
            t[k++]=i++;
        int j;
        double x;
        for(i=0;i<k-1;++i)
        for(j=i+1;j<i+8 && j<k;++j)
        {
            x= sqrt(( v[t[j]].x - v[t[i]].x ) * ( v[t[j]].x - v[t[i]].x ) + ( v[t[j]].y - v[t[i]].y ) * ( v[t[j]].y - v[t[i]].y ));
            mn=min(mn,x);
        }
        return mn;
    }
    else
    {
        double mn=1e9;
        int i,j;
        for(i=s;i<d-1;++i)
            for(j=i+1;j<d;++j)
            mn=min(mn,sqrt(( v[i].x - v[j].x ) * ( v[i].x - v[j].x ) + ( v[i].y - v[j].y ) * ( v[i].y - v[j].y ) ) );
        return mn;
    }
}
int main()
{
    freopen("cmap.in","r",stdin);
    freopen("cmap.out","w",stdout);
    int i;
    scanf("%d",&n);
    for(i=0;i<n;++i)
        scanf("%lld%lld",&v[i].x,&v[i].y);
    sort(v,v+n,compare);
    double x=(dist(0,n));
    printf("%lf",x);
}