Cod sursa(job #1820695)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 2 decembrie 2016 03:01:18
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

ifstream in("cmap.in");
ofstream out("cmap.out");
int n;
vector <pair<int,int> > coords1, coords2;

bool compare1(pair <int,int> C1, pair <int,int> C2)
{
    if ((C1.first < C2.first) || ((C1.first == C2.first) && (C1.second < C2.second)))
        return true;
    return false;
}

bool compare2(pair <int,int> C1, pair <int,int> C2)
{
    if ((C1.second < C2.second) || ((C1.second == C2.second) && (C1.first < C2.first)))
        return true;
    return false;
}

double dist(pair <int,int> C1, pair <int,int> C2)
{
    return sqrt(pow((C1.first - C2.first), 2) + pow((C1.second - C2.second), 2));
}

int main()
{
    in >> n;
    int x, y;
    for (int i = 1; i <= n; i++)
    {
        in >> x >> y;
        coords1.push_back(make_pair(x, y));
        coords2.push_back(make_pair(x, y));
    }
    in.close();
    sort(coords1.begin(), coords1.end(), compare1);
    sort(coords2.begin(), coords2.end(), compare2);
    double best = dist(coords1[0], coords1[1]), act;
    for (unsigned int i = 2; i < coords1.size(); i++)
    {
        act = dist(coords1[i], coords1[i - 1]);
        if (act < best)
            best = act;
    }
    for (unsigned int i = 1; i < coords2.size(); i++)
    {
        act = dist(coords2[i], coords2[i - 1]);
        if (act < best)
            best = act;
    }
    out << best << "\n";
    out.close();
    return 0;
}