Cod sursa(job #1233924)

Utilizator tudormaximTudor Maxim tudormaxim Data 26 septembrie 2014 12:49:48
Problema Adapost 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include<cstdio>
#include<cmath>
#define L 50001
using namespace std;
struct COORDONATE{double x, y;} v[L];
int n, i;
double x, y, Dabs, step;

inline double DIST(double x, double y){
	double s = 0.0;
	for (int i = 1; i <= n; ++i){
		s += sqrt((x - v[i].x)*(x - v[i].x) + (y - v[i].y)*(y - v[i].y));
	}
	return s;
}

int main() {
	freopen("adapost2.in", "r", stdin);
	freopen("adapost2.out", "w", stdout);
	scanf("%d", &n);
	for (i = 1; i <= n; ++i) {
		scanf("%lf%lf", &v[i].x, &v[i].y);
		x += v[i].x;
		y += v[i].y;
	}
	x /= n;
	y /= n;
	step = 200;
	Dabs = DIST(x, y);
	while (step>0.0001) {
        bool ok = 1;
		double dx[] = { step, -step, 0, 0 };
		double dy[] = { 0, 0, step, -step };
		for (i = 0; i <= 3 && ok ; ++i){
			if (DIST(x + dx[i], y + dy[i])<Dabs){
				Dabs = DIST(x + dx[i], y + dy[i]);
				x = x + dx[i];
				y = y + dy[i];
				ok = 0;
			}
		}
		if (ok)
			step /= 2;
	}
	printf("%.4lf %.4lf\n", x, y);
	fclose(stdin);
	fclose(stdout);
	return 0;
}