Cod sursa(job #2289902)

Utilizator CristyXtremeSimion Cristian CristyXtreme Data 25 noiembrie 2018 14:53:47
Problema Adapost 2 Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <stdio.h>
#include <math.h>

using namespace std;

double distanta(double *x, double *y, int n, double X, double Y) {
    double suma = 0;
    for (int i = 0; i < n; i++)
        suma += sqrt((X - x[i]) * (X - x[i]) + (Y - y[i]) * (Y - y[i]));
    return suma;
}

void caut(double *x, double *y, int n) {
    double X = 500, Y = 500, pas = 500, semnX[4] = {0, 1, 0, -1}, semnY[4] = {1, 0, -1, 0}, min = distanta(x, y, n, X, Y);
    double x_prezent, y_prezent, dist;
    bool update;
    while (pas > 0.0000001) {
        update = false;
        for (int i = 0; i < 4; i++) {
            x_prezent = X + pas * semnX[i];
            y_prezent = Y + pas * semnY[i];
            dist = distanta(x, y, n, x_prezent, y_prezent);
            if (dist < min) {
                min = dist;
                X = x_prezent;
                Y = y_prezent;
                update = true;
                break;
            }
        }
        if (!update)
            pas /= 2;
    }
    printf("%lf %lf", X, Y);
}

int main()
{
    freopen("adapost2.in", "r", stdin);
    freopen("adapost2.out", "w", stdout);
    int n;
    scanf("%d", &n);
    double x[n], y[n];
    for (int i = 0; i < n; i++)
        scanf("%lf %lf", &x[i], &y[i]);
    caut(x, y, n);
    return 0;
}