Cod sursa(job #2117946)

Utilizator LucaSeriSeritan Luca LucaSeri Data 29 ianuarie 2018 20:03:12
Problema Cele mai apropiate puncte din plan Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 2.09 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5 + 10;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

double dist(pair< int, int >& a, pair< int, int >& b){
    return sqrt(1.0*(a.first - b.first) * (a.first - b.first) + 1.0*(a.second - b.second) * (a.second - b.second));
}

pair< int, int > v[MAXN];

int main(){
    InParser f("cmap.in");
    ofstream g("cmap.out");
    srand(time(NULL));

    int n;
    f >> n;
    if(rand() & 1){
        for(int i = 0; i < n; ++i){
            f >> v[i].second >> v[i].first;
        }
    } else {
        for(int i = 0; i < n; ++i){
            f >> v[i].first >> v[i].second;
        }
    }
    sort(v, v + n);
    double best = dist(v[0], v[1]);

    for(int i = 0; i < n; ++i){
        for(int j = i + 1; j < n && v[j].first - v[i].first < best; ++j){
            best = min(best, dist(v[i], v[j]));
        }
    }

    g << setprecision(10) << fixed << best;
    return 0;
}