Cod sursa(job #2074082)

Utilizator priestnoobFMI - Dan Nema priestnoob Data 24 noiembrie 2017 06:48:43
Problema Cele mai apropiate puncte din plan Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <iostream>
#include <fstream>
#include <math.h>
#include <utility>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

ifstream f("cmap.in");
ofstream g("cmap.out");

#define nmax 100001
#define inf 10000000000

pair<long,long> aux[nmax];

bool comp(pair <long,long> a, pair <long,long> b)
{
    if(a.first<b.first) return 1;
    if(a.first==b.first && a.second<b.second) return 1;
    return 0;
}

bool compy(pair<long,long> a, pair<long, long> b)
{
    return a.second<b.second;
}

pair<long, long> v[nmax];

inline double long d(pair<long,long> a, pair<long, long> b)
{
    long long rez= pow((a.first-b.first),2) + pow((a.second-b.second),2);
    return sqrt(double(rez));
}

long double solve(int a, int b)
{
    if(a==b) return inf;
    if(b-a==1) return d(v[a],v[b]);
    int m = (a+b)/2;
    long double rez=min(solve(a,m),solve(m+1,b));
    int k=1;
    for(int i=a;i<=m;++i)
        if(v[m].first-v[i].first<rez) aux[k]=v[i],++k;
    for(int i=m+1;i<=b;++i)
        if(v[i].first-v[i].first<rez) aux[k]=v[i],++k;
    sort(aux+1,aux+k+1,compy);
    for(int j=1;j<=k;++j)
        for(int i=1;i<8&&((j+i)<=k);++i)
        rez=min(rez,d(aux[j],aux[i+j]));
    return rez;
}

int main()
{
    int n;
    pair<long,long> aux1;
    f>>n;
    for(int i=1;i<=n;++i)
    {
        f>>aux1.first>>aux1.second;
        v[i]=aux1;
    }
    sort(v+1,v+n+1,comp);
    long double rez = solve(1,n);
    g<<fixed<<setprecision(8)<<rez;
}