Cod sursa(job #1590447)

Utilizator howsiweiHow Si Wei howsiwei Data 5 februarie 2016 08:40:11
Problema Cele mai apropiate puncte din plan Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <complex>
using namespace std;
typedef complex<double> Point;
const double oo = 1e9;

struct cmpx {
	bool operator () (const Point& a, const Point& b) const {
		return real(a) < real(b);
	}
};

struct cmpy {
	bool operator () (const Point& a, const Point& b) const {
		return imag(a) < imag(b);
	}
};

double sqd(const Point& a, const Point& b) {
	return norm(b-a);
}

int main() {
	freopen("cmap.in", "r", stdin);
	freopen("cmap.out", "w", stdout);
	cin.sync_with_stdio(false);
	int n;
	cin >> n;
	vector<Point> ps(n);
	for (int i = 0; i < n; i++) {
		double x, y;
		cin >> x >> y;
		ps[i] = {x,y};
	}
	sort(ps.begin(), ps.end(), cmpx());
	double best = 4*oo;
	double sqbest = pow(best,2);
	set<Point,cmpy> ordy;
	ordy.emplace(oo, oo+best);
	ordy.emplace(oo, -oo-best);
	int lx = 0;
	for (int i = 0; i < n; i++) {
		while (real(ps[lx]) <= real(ps[i])-best) {
			ordy.erase(ps[lx++]);
		}
		auto it0 = ordy.insert(ps[i]).first;
		for (auto it = next(it0); imag(*it) < imag(ps[i])+best; ++it) {
			if (sqbest > sqd(ps[i], *it)) {
				sqbest = sqd(ps[i], *it);
				best = sqrt(sqbest);
			}
		}
		for (auto it = prev(it0); imag(*it) > imag(ps[i])-best; --it) {
			if (sqbest > sqd(ps[i], *it)) {
				sqbest = sqd(ps[i], *it);
				best = sqrt(sqbest);
			}
		}
	}
	printf("%.6f\n", best);
}