Cod sursa(job #1546623)

Utilizator sulzandreiandrei sulzandrei Data 8 decembrie 2015 13:53:34
Problema Cele mai apropiate puncte din plan Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 3.12 kb
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
ifstream in("cmap.in");
ofstream out("cmap.out");
struct Point
{
    int x,y;
    Point(int x=  0,int y = 0 )
    {
        x = this->x;
        y = this->y;
    }
    bool operator ==(Point e);
    friend istream& operator >>(istream &i,Point & p);
    friend ostream& operator <<(ostream& o,Point p);
};
ostream& operator <<(ostream& o,Point p)
{
    o<<p.x<<" "<<p.y;
    return o;
}
istream& operator >>(istream &i,Point & p)
{
    i>>p.x>>p.y;
    return i;
}
bool Point::operator ==(Point e)
{
    if (this->x == e.x && this->y == e.y)
        return true;
    return false;
}
long double distance(long long  xa, long long  ya , long long  xb, long long  yb)
{
    return sqrt( (xb - xa)*(xb - xa) + (yb - ya)*(yb - ya));
}
Point X[100003];
long double distanceBetweenClosestPairofPoint(int st,int dr, vector<Point> Y)
{
    if(  dr - st + 1 > 3)
    {
        int mij = (st+dr)/2;

        vector<Point> Ys,Yd;
        for(vector<Point>::iterator p = Y.begin(); p!=Y.end() ; p++)
            if ( p->x <X[mij].x)
                Ys.push_back( *p );
            else
            {
                if (p->x == X[mij].x)
                    if (p->y <= X[mij].y)
                        Ys.push_back(*p);
                    else
                        Yd.push_back(*p);
                else
                    Yd.push_back(*p);
            }
        //cout<<" nr elemente: Ys"<<Ys.size()<<"  nr elemente Yd"<<Yd.size()<<'\n';
        long double mins,mind,minm;

        mins = distanceBetweenClosestPairofPoint(st,mij,Ys);
        mind = distanceBetweenClosestPairofPoint(mij+1,dr,Yd);
        minm = min(mins,mind);
        vector<Point> M;
        for(auto p = Y.begin() ; p <= p+min(dr-st+1,7) && p!= Y.end() ; p++)
            for(int i =  1 ; i <= min(dr-st+1,7) && p + i !=Y.end() ; i++)
                minm = min( distance(p->x,p->y,(p+i)->x , (p+i)->y),minm);
        return minm;
    }
    else
    {
        long double d = distance(X[st].x,X[st].y,X[st+1].x,X[st+1].y);
        //cout<<'\n'<<st<<" "<<dr<<" "<<setprecision(6)<<d<<'\n';
        for(int i = st ; i< dr ; i++)
            for(int j = i+1 ; j <= dr; j++)
                d = min(distance(X[i].x,X[i].y,X[j].x,X[j].y),d);
        return d;
    }

}
int main()
{
    int n;
    vector<Point> Y;
    in>>n;
    for(int i = 1 ; i <= n  ; i++)
        in>>X[i],Y.push_back(X[i]);
    sort(X+1,X+n+1,[](Point a,Point b)
    {
         if (a.x < b.x)
            return true;
         else
         {
             if (a.x ==b.x)
                if (a.y <= b.y)
                    return true;
                else
                    return false;
             return false;
         }
    });
    sort(Y.begin() , Y.end() ,[](Point a, Point b){ return (a.y < b.y);});
    out<<setprecision(6)<<fixed<<distanceBetweenClosestPairofPoint(1,n,Y);
    //cout<<distance(X[992].x,X[992].y,X[993].x,X[993].y)<<"\n";
    //cout<<X[992]<<" "<<X[993];

    return 0;
}