Cod sursa(job #1645782)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 10 martie 2016 13:45:15
Problema Cele mai apropiate puncte din plan Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <cmath>
#include <fstream>
#include <iomanip>
#include <algorithm>

using namespace std;

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

struct pct
{
    double x, y;
}a[100001];
int i, j, n;
long double dmin = 1999999999, w;

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

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

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

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

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

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

    g << fixed << setprecision(6) << dmin;
    return 0;
}