Cod sursa(job #1645733)

Utilizator tudorgalatanRoman Tudor tudorgalatan Data 10 martie 2016 13:33:10
Problema Cele mai apropiate puncte din plan Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
#include <cmath>
#include <algorithm>
#include <iomanip>

using namespace std;

struct punct
{
    long double x, y;
};

punct v[100005];

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

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

int main ()
{
    ifstream fin  ("cmap.in");
    ofstream fout ("cmap.out");
    int n, j;
    fin >> n;
    int i;
    for (i=1; i<=n; i++)
        fin >> v[i].x >> v[i].y;
    sort(v+1,v+n+1,cmp);
    long double mi, w;
    mi = dist(v[1],v[2]);
    for (i=3; i<=n; i++)
    {
        j = i-1;
        while (v[i].x-v[j].x < mi and j>=1)
        {
            w = dist (v[i],v[j]);
            if (mi > w)
                mi = w;
            j -= 1;
        }
    }
    fout << fixed << setprecision(6) << mi;
    return 0;
}