Problem p01277 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
const double EPS = 1e-9;
typedef complex<double> P;
typedef array<P, 2> L;
P vec(L l) { return l[1] - l[0]; }
int sign(double x) { return x > EPS ? 1 : x < -EPS ? -1 : 0; }
double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
namespace std {
bool operator<(const L &l, const L &m) {
  double a1 = arg(vec(l));
  double a2 = arg(vec(m));

  if (sign(a1) < 0)
    a1 += M_PI;
  if (sign(a2) < 0)
    a2 += M_PI;
  if (a1 > M_PI - EPS)
    a1 -= M_PI;
  if (a2 > M_PI - EPS)
    a2 -= M_PI;

  if (sign(a1 - a2) != 0)
    return sign(a1 - a2) < 0;

  double th = (a1 + a2) / 2.0;

  P p1 = l[0] * polar(1.0, -th);
  P p2 = m[0] * polar(1.0, -th);
  double b1 = p1.imag();
  double b2 = p2.imag();

  return sign(b1 - b2) < 0;
}
bool operator==(const L &l, const L &m) { return !(l < m) && !(m < l); }
bool operator<(const P &a, const P &b) {
  if (sign(a.real() - b.real()) != 0)
    return sign(a.real() - b.real()) < 0;
  return sign(a.imag() - b.imag()) < 0;
}
bool operator==(const P &a, const P &b) {
  return sign(a.real() - b.real()) == 0 && sign(a.imag() - b.imag()) == 0;
}
} // namespace std

P proj(L l, P p) {
  double t = dot(p - l[0], vec(l)) / norm(vec(l));
  return l[0] + t * vec(l);
}

// 点pの直線lに関する反射
P refl(L l, P p) { return 2.0 * proj(l, p) - p; }

bool check(L l, const vector<P> &ps) {
  int N = ps.size();
  bool ok = true;
  int online = 0;
  for (int i = 0; i < N; i++) {
    P rp = refl(l, ps[i]);
    if (ps[i] == rp)
      online++;
    if (!binary_search(ps.begin(), ps.end(), rp)) {
      ok = false;
      break;
    }
    if (online > 2) {
      ok = false;
      break;
    }
  }
  return ok;
}

int main() {
  int N;
  while (cin >> N && N > 0) {
    vector<P> ps(N);
    REP(i, N) {
      double x, y;
      cin >> x >> y;
      ps[i] = {x, y};
    }

    sort(ps.begin(), ps.end());

    set<L> ls;
    for (int i = 0; i + 1 < N; i++) {
      L l = {ps[i], ps[i + 1]};
      ls.insert(l);
    }

    if (ls.size() == 1) {
      cout << "No" << endl;
      continue;
    }

    bool ans = false;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < N; j++)
        if (i != j) {
          P mp = (ps[j] + ps[i]) / 2.0;
          P vp = (ps[j] - ps[i]) * polar(1.0, M_PI / 2.0);
          L l = {mp, mp + vp};
          if (check(l, ps))
            ans = true;
        }
    }

    cout << (ans ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
const double EPS = 1e-9;
typedef complex<double> P;
typedef array<P, 2> L;
P vec(L l) { return l[1] - l[0]; }
int sign(double x) { return x > EPS ? 1 : x < -EPS ? -1 : 0; }
double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
namespace std {
bool operator<(const L &l, const L &m) {
  double a1 = arg(vec(l));
  double a2 = arg(vec(m));

  if (sign(a1) < 0)
    a1 += M_PI;
  if (sign(a2) < 0)
    a2 += M_PI;
  if (a1 > M_PI - EPS)
    a1 -= M_PI;
  if (a2 > M_PI - EPS)
    a2 -= M_PI;

  if (sign(a1 - a2) != 0)
    return sign(a1 - a2) < 0;

  double th = (a1 + a2) / 2.0;

  P p1 = l[0] * polar(1.0, -th);
  P p2 = m[0] * polar(1.0, -th);
  double b1 = p1.imag();
  double b2 = p2.imag();

  return sign(b1 - b2) < 0;
}
bool operator==(const L &l, const L &m) { return !(l < m) && !(m < l); }
bool operator<(const P &a, const P &b) {
  if (sign(a.real() - b.real()) != 0)
    return sign(a.real() - b.real()) < 0;
  return sign(a.imag() - b.imag()) < 0;
}
bool operator==(const P &a, const P &b) {
  return sign(a.real() - b.real()) == 0 && sign(a.imag() - b.imag()) == 0;
}
} // namespace std

P proj(L l, P p) {
  double t = dot(p - l[0], vec(l)) / norm(vec(l));
  return l[0] + t * vec(l);
}

// 点pの直線lに関する反射
P refl(L l, P p) { return 2.0 * proj(l, p) - p; }

int main() {
  int N;
  while (cin >> N && N > 0) {
    vector<P> ps(N);
    REP(i, N) {
      double x, y;
      cin >> x >> y;
      ps[i] = {x, y};
    }

    map<L, short> cnt;
    REP(i, N) REP(j, N) if (i != j) {
      L l = {ps[i], ps[j]};
      cnt[l] += 1;
    }

    if (cnt.size() == 1) {
      cout << "No" << endl;
      continue;
    }

    REP(i, N) REP(j, N) if (i != j) {
      P mp = (ps[i] + ps[j]) / 2.0;
      P vp = (ps[j] - ps[i]) * polar(1.0, M_PI / 2.0);
      L l = {mp, mp + vp};
      cnt[l] += 1;
    }

    // set<P> s;
    // REP(i, N) s.insert(ps[i]);
    sort(ps.begin(), ps.end());
    bool ans = false;
    // for(auto cp : cnt) {
    //     //cout << cp.first[0] << " " << cp.first[1] << " " << cp.second <<
    //     endl;
    // }
    for (auto cp : cnt)
      if (cp.second >= N - 2) {
        bool ok = true;
        int online = 0;
        for (int i = 0; i < N; i++) {
          P rp = refl(cp.first, ps[i]);
          if (ps[i] == rp)
            online++;
          if (!binary_search(ps.begin(), ps.end(), rp)) {
            ok = false;
            break;
          }
          if (online > 2) {
            ok = false;
            break;
          }
        }
        if (ok)
          ans = true;
        /*
        if(ok) {
            cout << cp.first[0] << " " << cp.first[1] << endl;
            DrawLine(cp.first);
            for(int i = 0; i < N; i++) {
                DrawPoint(ps[i]);
                P rp = refl(cp.first, ps[i]);
                assert(s.count(rp));
                cout << ps[i] << "<->" << rp << "(" << s[rp] << ")" << endl;
                assert(s[ refl(cp.first, rp) ] == i);
            }
            break;
        }
        */
      }

    cout << (ans ? "Yes" : "No") << endl;
  }
  return 0;
}