Cod sursa(job #2058374)

Utilizator gorneanu.andreiFMI Gorneanu Andrei gorneanu.andrei Data 5 noiembrie 2017 16:06:58
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.91 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <vector>
#define inf 9999999
#define MAXN 100500
using namespace std;

struct punct{
    int x, y;

}a[MAXN], b[MAXN], v[MAXN];

int n;

bool cmpX(punct c,punct d){

    return (c.x < d.x);
}

bool cmpY(punct c,punct d){

    return (c.y < d.y);
}
double dist(punct c,punct d){

    return (c.x - d.x) * (c.x - d.x) + (c.y - d.y) * (c.y - d.y);
}

double solve(int st,int dr){

    if(dr - st <= 0)
        return inf;
    else if(dr - st == 1){
        if(a[st].y > a[dr].y)
            swap(a[st],a[dr]);
        return dist(a[st],a[dr]);
    }

    int mid = (st + dr) >> 1;
    double d = min(solve(st,mid),solve(mid + 1,dr));

    vector<punct> c;

    int c1 = st, c2 = mid + 1;

    while(c1 <= mid && c2 <= dr){
        if(b[c1].y > b[c2].y){
            c.push_back(b[c2]);
            ++c2;
        }
        else{
            c.push_back(b[c1]);
            ++c1;
        }
    }

    while(c1 <= mid){
        c.push_back(b[c1]);
        ++c1;
    }

    while(c2 <= dr){
        c.push_back(b[c2]);
        ++c2;
    }

    for(int i = st;i <= dr; ++i)
        a[i] = c[i - st];

    vector<punct> aux;
    double d1 = sqrt(d);

    for(int i = st;i <= dr; ++i)
        if(abs(a[mid].x - a[i].x) <= d1)
            aux.push_back(a[i]);

    int len = aux.size();
    int j;
    for(int i = 0;i < len - 1; ++i)
        for(j = i + 1;j < i + 8 && j < len; ++j)
            d = min(d,dist(aux[i],aux[j]));

    return d;
}

int main(){

    fstream f("cmap.in",ios::in);
    fstream g("cmap.out",ios::out);

    f >> n;

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

    sort(a,a + n,cmpX);

    double answ = sqrt(solve(0,n - 1));
    g << fixed << setprecision(6) << answ;

    f.close();
    g.close();
    return 0;
}