Cod sursa(job #2275322)

Utilizator Wh1plashOvidiu Taralesca Wh1plash Data 3 noiembrie 2018 01:11:26
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
// O(n^2)

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>

#define x first
#define y second
#define INF 10000000000.0
using namespace std;
ifstream in("cmap.in");
ofstream out("cmap.out");
typedef pair<int,int> punct;
vector<punct> v,vnou;
int n;
bool cmpy(punct a, punct b){ return a.y > b.y; }
bool cmpx(punct a, punct b){ return a.x > b.x; }
inline double dist(punct a, punct b){ return (double)sqrt((long long)(a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); }

double divide(int st, int dr){
    int m = (st+dr)/2;
    int n = dr-st+1;
    if(n == 1) return INF;
    else if( n==2) return dist(v[st],v[dr]);
    else {
        double dmin = min(divide(st,m), divide(m,dr));
        double mij = (double) (v[st].y + v[dr].y) / 2;
        vnou.clear();
        for(int i = st; i<=dr; i++){
            if(mij - dmin <= v[i].y || mij + dmin >= v[i].y ){
                vnou.push_back(v[i]);
            }
        }
        sort(vnou.begin(), vnou.end(), cmpx);
        double dmin_banda = INF;
        if(!vnou.empty()) {
            for (int j = 0; j < vnou.size() - 1; j++) {
                for (int k = j + 1; k < min((int) vnou.size(), j + 8); k++) {
                    dmin_banda = min(dmin_banda, dist(vnou[j], vnou[k]));
                }
            }
        }
        return min(dmin,dmin_banda);
    }
}

int main(){
    in >> n;
    v.resize(n);
    for(int i=0;i<n;i++)
        in >> v[i].x >> v[i].y;
    sort(v.begin(),v.end(),cmpy);
    out << divide(0,n-1);
    return 0;
}