Cod sursa(job #2069927)

Utilizator StriddRobert Stridd Data 18 noiembrie 2017 23:01:10
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.8 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <cmath>

using namespace std;


struct Point{
    long long x;
    long long y;

    const Point& operator=(const Point& P)
    {
        x = P.x;
        y = P.y;
        return P;
    }
};

long long returnDist(Point& P1, Point& P2)
{
    return  (long long) (P2.x - P1.x) * (long long) (P2.x - P1.x) +
            (long long) (P2.y - P1.y) * (long long) (P2.y - P1.y);
}

int compByX(const Point& P1,const Point& P2)
{
    return P1.x < P2.x;
}

int compByY(const Point& P1,const Point& P2)
{
    return P1.y < P2.y;
}

vector<Point> Points;
vector<Point> PointsOY;

// Creare alt vector sortat dupa OY

int sort3(Point& P1, Point& P2, Point& P3)
{
    Point X;
    if(P1.y > P2.y && P1.y > P3.y && P2.y > P3.y) // 3 2 1
    {
        X = P1;
        P1 = P3;
        P3 = X;
    }
    if(P1.y > P2.y && P1.y > P3.y && P2.y < P3.y)
    {
        X = P1;
        P1 = P2;
        P2 = X;
        X = P3;
        P3 = P2;
        P2 = X;
    }
    if(P1.y < P2.y && P1.y > P3.y )
    {
        X = P3;
        P3 = P1;
        P1 = X;
        X = P2;
        P2 = P3;
        P3 = X;
    }
    if(P1.y < P2.y && P1.y < P3.y && P2.y > P3.y)
    {
        X = P2;
        P2 = P3;
        P3 = X;
    }
    return 0;
}


long long divideClosPoints(long left, long right)
{
    if(right - left == 1)
    {
        if(PointsOY[left].y > PointsOY[right].y )
        {
            Point X = PointsOY[left];
            PointsOY[left] = PointsOY[right];
            PointsOY[right] = X;
        }
        return returnDist(Points[left], Points[right]);
    }
    if(right - left == 2)
        {
            sort3(PointsOY[left], PointsOY[left + 1], PointsOY[right]);
            return min(returnDist(Points[left], Points[left+1]),
            min(returnDist(Points[left+1], Points[right]),
                returnDist(Points[left], Points[right])));
        }

    long middle = (left + right) / 2;
    long long dLeft = divideClosPoints(left, middle);
    long long dRight = divideClosPoints(middle + 1, right);

    long long d = min(dLeft, dRight);

    long i, j;

    vector<Point> verticalSec;

    auto delta = (int) ceil( sqrt(d) );

    //i = left;
    i = left;
    j = middle + 1;

    vector<Point> sol;

    while(i <= middle && j <= right)
    {
        if(PointsOY[i].y < PointsOY[j].y)
        {
            sol.push_back(PointsOY[i]);
            i++;
        }
        else
        {
            sol.push_back(PointsOY[j]);
            j++;
        }
    }

    while(i <= middle)
    {
        i++;
        sol.push_back(PointsOY[i]);
    }
    while(j <= right)
    {
        j++;
        sol.push_back(PointsOY[j]);
    }
    for(i = left, j = 0; i <= right; i++, j++)
    {
        PointsOY[i] = sol[j];
    }

    for(i = left; i <= right; i++)
    {
        if(abs(PointsOY[i].x - PointsOY[middle].x) <= delta)
                 verticalSec.push_back(PointsOY[i]);
    }

    long long sizeV = verticalSec.size();

    for(i = 0; i < sizeV ; i++)
        for(j = i + 1; j <= i + 7 && j < sizeV ; j++)
        {
            d = min(d, returnDist(verticalSec[i],verticalSec[j]) );
        }
    return d;
}

int main()
{
    ifstream fileRead("cmap.in");
    ofstream fileWrite("cmap.out");
    long long nrPoints, i;

    Point P;

    fileRead >> nrPoints;

    for(i = 0 ; i < nrPoints; i++)
    {
        fileRead >> P.x >> P.y;
        Points.push_back(P);
        PointsOY.push_back(P);
    }

    sort(Points.begin(),Points.end(),compByX);
    fileWrite << fixed << setprecision(6) << sqrt(divideClosPoints(0,Points.size() - 1));

    fileRead.close();
    fileWrite.close();
}