Cod sursa(job #2133831)

Utilizator sandu.m.mdMorari Sandu sandu.m.md Data 17 februarie 2018 12:55:55
Problema Cele mai apropiate puncte din plan Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <iomanip>

#define per pair<double, double>

using namespace std;
ifstream fin("cmap.in");
ofstream fout("cmap.out");

vector<per> vec;
int n;
double x, y;

bool cmp(per a, per b)
{   
    bool ch = false;
    if (a.first < b.first)
        ch = true;
    else if (a.first == b.first)
        if (a.second < b.second)
            ch = true;
    return ch;
}

int main()
{

    fin >> n;
    for (int i = 0; i < n; i++)
    {
        fin >> x >> y;
        vec.push_back(make_pair(x, y));
    }

    sort(vec.begin(), vec.end(), cmp);

    double minim = sqrt(pow(vec[0].first - vec[1].first, 2) + pow(vec[0].second - vec[1].second, 2));

    for (int i = 1; i < n - 1; i++)
    {
        if (minim > sqrt(pow(vec[i].first - vec[i + 1].first, 2) + pow(vec[i].second - vec[i + 1].second, 2)))
        {
            minim = sqrt(pow(vec[i].first - vec[i + 1].first, 2) + pow(vec[i].second - vec[i + 1].second, 2));
        }
    }
    fout << setprecision(8) << minim << "\n";
    return 0;
}