Cod sursa(job #2079939)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 2 decembrie 2017 00:45:20
Problema Adapost 2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.18 kb
#include <bits/stdc++.h>
using namespace std;

constexpr int DIM = 50005;
constexpr int MAX_COORDINATE = 1000;
constexpr  double EPS = 1e-4;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, 1, -1};

struct point {
    point(double x = 0, double y = 0) :
        x(x), y(y) {
    }
    double x, y;
};

int point_count;
point points[DIM];

inline point center_of_mass() {
    point center;
    for (int i = 1; i <= point_count; ++i) {
        center.x += points[i].x;
        center.y += points[i].y;
    }

    center.x /= point_count;
    center.y /= point_count;

    return center;
}

inline double euclidian_distance(const point& first, const point& second) {
    return sqrt( (first.x - second.x)*(first.x - second.x) + (first.y - second.y)*(first.y - second.y) );
}

inline double evaluate(const point& center) {
    double res = 0;
    for (int i = 1; i <= point_count; ++i)
        res += euclidian_distance(points[i], center);
    return res;
}

inline void solve() {
    cin >> point_count;
    for (int i = 1; i <= point_count; ++i)
        cin >> points[i].x >> points[i].y;

    point best_point = center_of_mass();
    double best_evaluation = evaluate(best_point);

    for (double jump = MAX_COORDINATE; jump > EPS; jump /= 2) {
        double candidate_evaluation = 1.0 * numeric_limits< int >::max();
        point candidate_point;

        for (int dir = 0; dir < 4; ++dir) {
            point current_point(best_point.x + dx[dir]*jump, best_point.y + dy[dir]*jump);
            double current_evaluation = evaluate(current_point);

            if (current_evaluation < candidate_evaluation) {
                candidate_evaluation = current_evaluation;
                candidate_point = current_point;
            }
        }

        if (candidate_evaluation < best_evaluation) {
            best_evaluation = candidate_evaluation;
            best_point = candidate_point;
            jump *= 2;
        }
    }

    cout << setprecision(4) << fixed << best_point.x << ' ' << best_point.y << endl;
}

int main() {
    assert(freopen("adapost2.in", "r", stdin));
    assert(freopen("adapost2.out", "w", stdout));
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}