Cod sursa(job #1083533)

Utilizator andreivFMI - vacaroiu andrei andreiv Data 16 ianuarie 2014 02:38:00
Problema Adapost 2 Scor 95
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.03 kb
#include<cmath>
#include<cstdio>
#define maxn 50005
#define eps 0.0001

double x[maxn],y[maxn],salt;
double px=0.0,py=0.0,q=0.0;
int i,n;

inline double dist(double a,double b){
       double d=0.0,x1,y1;

       for(int i=1;i<=n;i++){
          x1=x[i]-a; x1*=x1;
          y1=y[i]-b; y1*=y1;
          d+=sqrt(x1+y1);
          }

       return d;
}

int main(){
    freopen("adapost2.in","r",stdin);
    freopen("adapost2.out","w",stdout);

    scanf("%d",&n);

    for(i=1;i<=n;i++) {
                       scanf("%lf %lf",&x[i],&y[i]);
                       px+=x[i];
                       py+=y[i];
                      }

    px/=n; py/=n; q=dist(px,py); salt=500;

    while(salt>eps)
     {
      if (dist(px,py+salt)<q) { py+=salt; q=dist(px,py); continue; };
      if (dist(px,py-salt)<q) { py-=salt; q=dist(px,py); continue; };
      if (dist(px+salt,py)<q) { px+=salt; q=dist(px,py); continue; };
      if (dist(px-salt,py)<q) { px-=salt; q=dist(px,py); continue; };
      salt/=2;
     }

    printf("%.4lf %.4lf",px,py);

    return 0;
}