Cod sursa(job #1564541)

Utilizator hrazvanHarsan Razvan hrazvan Data 9 ianuarie 2016 19:05:27
Problema Adapost 2 Scor 70
Compilator c Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <stdio.h>
#include <math.h>
#define MAXN 50000
#define BUFF (1 << 20)
FILE *in;
double l[MAXN], c[MAXN];
int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
char s[BUFF];
int pin = BUFF;

inline char getch(){
  if(pin == BUFF){
    fread(s, BUFF, 1, in);
    pin = 0;
  }
  pin++;
  return s[pin - 1];
}

inline double getnr(){
  char ch;
  double rez = 0;
  ch = getch();
  while(!(ch >= '0' && ch <= '9'))
    ch = getch();
  while(ch >= '0' && ch <= '9'){
    rez *= 10;
    rez += ch - '0';
    ch = getch();
  }
  double p = 1;
  if(ch == '.'){
    ch = getch();
    while(ch >= '0' && ch <= '9'){
      p /= 10;
      rez += p * (ch - '0');
      ch = getch();
    }
  }
  return rez;
}

inline double dist(double x, double y, int n){
  int i;
  double d = 0;
  for(i = 0; i < n; i++){
    d += sqrt((x - l[i]) * (x - l[i]) + (y - c[i]) * (y - c[i]));
  }
  return d;
}

int main(){
  in = fopen("adapost2.in", "r");
  int i;
  double n, x = 0, y = 0, pas;
  n = (int) getnr();
  for(i = 0; i < n; i++){
    l[i] = getnr();  c[i] = getnr();
    fscanf(in, "%lf%lf", &l[i], &c[i]);
    x += l[i];  y += c[i];
  }
  x /= n;  y /= n;
  pas = 2;
  while(pas > 0.00001){
    for(i = 0; i < 4; i++){
      while(dist(x + d[i][0] * pas, y + d[i][1] * pas, n) < dist(x, y, n)){
        x += d[i][0] * pas;
        y += d[i][1] * pas;
      }
    }
    pas /= 2;
  }
  FILE *out = fopen("adapost2.out", "w");
  fprintf(out, "%.4lf %.4lf", x, y);
  fclose(out);
  return 0;
}