Cod sursa(job #2072034)

Utilizator ViorelPopescuViorel Popescu ViorelPopescu Data 21 noiembrie 2017 12:35:05
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <math.h>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream f("cmap.in");
ofstream g("cmap.out");

struct punct
{
    double x, y;
}a[2000];

int i, j, n;
long double distantaMinima = 1000000, aux;

bool cmp(punct a, punct b)
{
    if (a.x < b.x)
        return 1;
    return 0;
}

long double dist(punct a, punct b)
{
    return sqrt((b.x-a.x)*(b.x-a.x) + (b.y-a.y)*(b.y-a.y));
}

int main()
{
    f >> n;
    for (i = 1; i <= n; i++){
        f >> a[i].x >> a[i].y;
    }

    sort (a+1, a+n+1, cmp);

    distantaMinima = dist(a[1], a[2]);

    for (i = 3; i <= n; i++)
    {
        j = i-1;
        while (a[i].x - a[j].x < distantaMinima && j >= 1)
        {
            aux = dist(a[i], a[j]);
            if (distantaMinima > aux) distantaMinima = aux;
            j--;
        }
    }

    g << distantaMinima;
    return 0;
}