Cod sursa(job #2089154)

Utilizator alex2kamebossPuscasu Alexandru alex2kameboss Data 16 decembrie 2017 11:14:33
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.83 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <climits>

using namespace std;
vector <pair<int,int>> p;
vector <pair<int,int>> l;
int n;
inline void citire()
{
    scanf("%d\n", &n);
    int x,y;
    for(int i=0;i<n;++i)
    {
        scanf("%d %d\n", &x,&y);
        p.push_back({x,y});
    }
    sort(p.begin(),p.end());
}
inline long long divide(int st, int dr)
{
    if(dr-st==1)
    {
        return ((p[st].first-p[dr].first)*(p[st].first-p[dr].first)+(p[st].second-p[dr].second)*(p[st].second-p[dr].second));
    }
    if(dr-st==2)
    {
        long long ret=((p[st].first-p[dr].first)*(p[st].first-p[dr].first)+(p[st].second-p[dr].second)*(p[st].second-p[dr].second));
        long long ret1=((p[st].first-p[st+1].first)*(p[st].first-p[st+1].first)+(p[st].second-p[st+1].second)*(p[st].second-p[st+1].second));
        long long ret2=((p[st+1].first-p[dr].first)*(p[st+1].first-p[dr].first)+(p[st+1].second-p[dr].second)*(p[st+1].second-p[dr].second));
        return min(ret,min(ret1,ret2));
    }
    int m=(dr+st)/2;
    long long s=min(divide(st,m),divide(m+1,dr));
    double d=sqrt(1.0*s);
    int dx=p[m].first;
    l.clear();
    for(int i=st;i<=dr;++i)
    {
        if(abs(p[i].first-dx)<=d)
            l.push_back(p[i]);
    }
    int siz=l.size();
    for(int i=0;i<siz-1;++i)
    {
        int merg=min(siz,i+7);
        for(int j=i+1;j<merg;++j)
        {
            s=min(s,1ll*((l[i].first-l[j].first)*(l[i].first-l[j].first)+(l[i].second-l[j].second)*(l[i].second-l[j].second)));
        }
    }
    return s;
}
int main()
{
    freopen("cmap.in","r",stdin);
    freopen("cmap.out","w",stdout);
    citire();
    cout<<sqrt(1.0*divide(0,n-1));
    //printf("%.6llf", sqrt(1.0*divide(0,n-1)));
    return 0;
}