Cod sursa(job #1758341)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 17 septembrie 2016 02:25:51
Problema Adapost 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <bits/stdc++.h>
using namespace std;
typedef double f64;

const int NMAX = 50005;

struct PTX {
    f64 x, y;

    inline PTX() { }
    inline PTX(f64 _x, f64 _y) {
        x = _x;
        y = _y;
    }

    friend ifstream& operator >> (ifstream &fs, PTX &arg) {
        fs>>arg.x>>arg.y;
    }
    friend ofstream& operator << (ofstream &fs, PTX &arg) {
        fs<<setprecision(5)<<arg.x<<' '<<arg.y;
    }
};

int n;
PTX pts[NMAX];

inline f64 dist(const PTX &a, const PTX &b) {
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}

inline f64 sdist(const PTX &arg) {
    f64 sum = 0.;
    for(int i=0; i<n; ++i)
        sum+= dist(arg, pts[i]);

    return sum;
}

int sx[] = {1, 0, 0, -1},
    sy[] = {0, 1, -1, 0};

int main(void) {
    ifstream fi("adapost2.in");
    ofstream fo("adapost2.out");
    PTX tmp, ant(0., 0.);
    f64 cdt, tdt;

    fi>>n;
    for(int i=0; i<n; ++i)
        fi>>pts[i];

    cdt = sdist(ant);
    for(f64 p=1000.; p>1e-6; p/=2.) {
        for(int i=0; i<4; ++i) {
            tmp.x = ant.x + p * sx[i];
            tmp.y = ant.y + p * sy[i];
            tdt   = sdist(tmp);

            if(tdt < cdt) {
                cdt = tdt;
                ant = tmp;
            }
        }
    }

    fo<<ant<<'\n';

    fi.close();
    fo.close();
    return 0;
}