Cod sursa(job #2110563)

Utilizator cella.florescuCella Florescu cella.florescu Data 20 ianuarie 2018 21:15:01
Problema Adapost 2 Scor 79
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <bits/stdc++.h>

using namespace std;

typedef pair <double, double> PII;
const int MAXN = 5e4;

PII v[MAXN];

inline double dist(PII A, PII B) {
  return sqrt((A.first - B.first) * (A.first - B.first) + (A.second - B.second) * (A.second - B.second));
}

int dirx[] = {-1, -1, 0, 1, 1, 1, -1}, diry[] = {0, 1, 1, 1, 0, -1, -1, -1};

int main()
{
    int n;
    ifstream fin("adapost2.in");
    fin >> n;
    PII P = {0.0, 0.0};
    for (int i = 0; i < n; ++i) {
      fin >> v[i].first >> v[i].second;
      P.first += v[i].first; P.second += v[i].second;
    }
    fin.close();
    P.first /= n; P.second /= n;
    double mv = 250;
    for (int step = 0; step < 30; ++step) {
      int dir;
      double minD = 1e10;
      for (int d = 0; d < 8; ++d) {
        double dst = 0.0;
        for (int i = 0; i < n; ++i)
          dst += dist({P.first + mv * dirx[d], P.second + mv * diry[d]}, v[i]);
        if (dst < minD) {
          minD = dst;
          dir = d;
        }
      }
      P.first += mv * dirx[dir]; P.second += mv * diry[dir];
      mv *= 0.58;
    }
    ofstream fout("adapost2.out");
    fout << setprecision(4) << fixed << P.first << " " << P.second;
    fout.close();
    return 0;
}