Mai intai trebuie sa te autentifici.

Cod sursa(job #2582494)

Utilizator PetrescuAlexandru Petrescu Petrescu Data 16 martie 2020 20:17:00
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.78 kb
#include <bits/stdc++.h>
#define MAX 100000 + 5
#define INF 2e9

using namespace std;

struct Point
{
    int x;
    int y;

    bool operator=(const Point &other)
    {
        x = other.x;
        y = other.y;
    }
};

Point xVector[MAX + 5], yVector[MAX + 5];

bool xCmp(Point a, Point b)
{
    return (a.x < b.x);
}

bool yCmp(Point a, Point b)
{
    return (a.y < b.y);
}

inline int dist(const Point &a, const Point &b)
{
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

int solve(int st, int dr)
{
    if(st >= dr)return INF;
    if(dr == st + 1)
    {
        if(yVector[st].x == yVector[dr].x && yVector[st].y > yVector[dr].y)
            swap(yVector[st], yVector[dr]);

        return dist(xVector[st], xVector[dr]);
    }

    int mij = (st + dr) >> 1;
    int best = min(solve(st, mij), solve(mij + 1, dr));

    sort(yVector + st, yVector + dr + 1, yCmp);

    vector<Point>rectangle;

    for(int i = st; i <= dr; i++)
        if(abs(yVector[i].x - xVector[mij].x) < best)rectangle.push_back(yVector[i]);

    for(int i = 0; i < rectangle.size() - 1; i++)
        for(int j = i + 1; j < rectangle.size() && j - i < 8; j++)
            best = min(best, dist(rectangle[i], rectangle[j]));

    return best;
}

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

    ios::sync_with_stdio(false);
    fin.tie(0);
    fout.tie(0);
    srand(time(NULL));

    int n;

    fin >> n;

    for(int i = 1; i <= n; i++)
        fin >> xVector[i].x >> xVector[i].y;

    sort(xVector + 1, xVector + 1 + n, xCmp);

    for(int i = 1; i <= n; i++)
        yVector[i] = xVector[i];

    fout << setprecision(8) << sqrt(solve(1, n));

    fin.close();
    fout.close();

    return 0;
}