Cod sursa(job #1971143)

Utilizator caprariuapCaprariu Alin-Paul caprariuap Data 19 aprilie 2017 21:07:41
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.05 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <queue>
#include <cmath>
using namespace std;

ifstream fin("cmap.in");
ofstream fout("cmap.out");

#define inf 3e+18
#define nmax 100010
#define eps 0.0000001
int n,i;
pair <int, int> a[nmax];
vector <pair <int, int> > x,y,z;
long double dist(pair <int, int> A, pair <int, int> B)
{
    return sqrt((long double)((A.first-B.first)*(A.first-B.first)+(A.second-B.second)*(A.second-B.second)));
}

class cmp1
{
public:
    bool operator()(pair <int, int> A, pair <int, int> B)
    {
        return (A.first<B.first||(A.first==B.first&&A.second<B.second));
    }
};

class cmp2
{
public:
    bool operator()(pair <int, int> A, pair <int, int> B)
    {
        return (A.second<B.second||(A.second==B.second&&A.first<B.first));
    }
};

long double divide(int st, int dr)
{
    if (dr<=st)
        return inf;
    int mij=(st+dr)/2;
    long double minim=min(divide(st,mij),divide(mij+1,dr));
    x.clear();
    y.clear();
    for (int i=mij; i>=st&&a[mij].first-a[i].first<=minim; i--)
        x.push_back(a[i]);
    for (int i=mij+1; i<=dr&&a[i].first-a[mij].first<=minim; i++)
        y.push_back(a[i]);
    sort(x.begin(),x.end(),cmp2());
    sort(y.begin(),y.end(),cmp2());
    int j,k;
    j=k=0;
    z.clear();
    while (j<x.size()&&k<y.size())
    {
        if (x[j].second<y[k].second)
        {
            z.push_back(x[j]);
            j++;
        }
        else
        {
            z.push_back(y[k]);
            k++;
        }
    }
    while (j<x.size())
        z.push_back(x[j]),j++;
    while (k<y.size())
        z.push_back(y[k]),k++;
    for (j=0; j<z.size()-1; j++)
        for (k=j+1; k<z.size()&&k-j<=8; k++)
            minim=min(minim,dist(z[j],z[k]));
    return minim;
}

int main()
{
    fin >> n;
    for (i=1; i<=n; i++)
        fin >> a[i].first >> a[i].second;
    sort(a+1,a+n+1,cmp1());
    fout << fixed << setprecision(7) << divide(1,n) << '\n';
}