Cod sursa(job #1059665)

Utilizator ELHoriaHoria Cretescu ELHoria Data 16 decembrie 2013 21:05:06
Problema Adapost 2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <math.h>
#include <cstdlib>

using namespace std;

const double eps = 1e-6;

double getSquaredDistsSum(pair<double, double> &p, vector < pair<double, double> > &v) {
	double ret = 0.0;
	for (const auto &s : v) {
		ret += sqrt((p.first - s.first) * (p.first - s.first) +
			(p.second - s.second) * (p.second - s.second));
	}
	return ret;
} 

inline int comp(const double &a, const double &b) {
	if (fabs(a - b) < eps) return 0;
	return b - a < eps ? 1 : -1;
}

int main()
{
	ifstream cin("adapost2.in");
	ofstream cout("adapost2.out");
	int n;
	cin >> n;
	vector < pair<double, double > > v(n);
	pair< double, double > ans = make_pair(0.0, 0.0);
	for (int i = 0; i < n; i++) {
		cin >> v[i].first >> v[i].second;
		ans.first += v[i].first;
		ans.second += v[i].second;
	}

	const int dx[] = { -1, -1, -1, 1, 1, 1, 0, 0 };
	const int dy[] = { -1, 1, 0, -1, 1, 0, -1, 1 };
	ans.first /= n;
	ans.second /= n;
	double currentDist = getSquaredDistsSum(ans, v);
//	cout << ans.first << " " << ans.second << "\n";
	double err = 1000.0;
	for (int i = 0; i < 20;i++, err /= 2.0) {	
		pair<double, double> next;
		for (int k = 0; k < 8; k++) {
			next.first = ans.first + err * dx[k];
			next.second = ans.second + err * dy[k];
			double lastDist = getSquaredDistsSum(next, v);
			if (comp(lastDist, currentDist) == -1) {
				ans = next;
				currentDist = lastDist;
			}
		}
	}
	cout.precision(4);
	cout << fixed << ans.first << " " << ans.second << "\n";	
	return 0;
}