Problem p01174 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;
#define curr(P, i) P[i]
#define next(P, i) P[(i + 1) % P.size()]
#define diff(P, i) (next(P, i) - curr(P, i))
typedef double number;
typedef complex<double> Point;
const double EPS = 1e-5;
typedef Point P;
typedef vector<Point> Polygon;
double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
double angle(P a, P b) { return arg(conj(a) * b); }
P rotate(P a, double b, P c = P()) { return (a - c) * polar(1.0, b) + c; }
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > +EPS)
    return +1; // 反時計回り
  if (cross(b, c) < -EPS)
    return -1; // 時計回り
  if (dot(b, c) < 0)
    return +2; // c--a--b の順番で一直線上
  if (norm(b) < norm(c))
    return -2; // a--b--c の順番で一直線上
  return 0;    // 点が線分ab上にある
}
int sign(double x) { return (x > EPS) - (x < -EPS); }
int comp(double x, double y) { return sign(x - y); }
namespace std {
bool operator<(const P &a, const P &b) {
  return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
  return comp(a.real(), b.real()) ? comp(a.real(), b.real()) < 0
                                  : comp(a.imag(), b.imag()) < 0;
}
}; // namespace std
bool near(Point a, Point b) {
  return comp(a.real(), b.real()) == 0 && comp(a.imag(), b.imag()) == 0;
}
bool equal(Polygon p, Polygon q) {
  sort(p.begin(), p.end());
  sort(q.begin(), q.end());
  REP(i, p.size()) {
    if (!near(p[i], q[i]))
      return false;
  }
  return true;
}
Polygon convex_hull(vector<P> ps, vector<int> &res) {
  int n = ps.size(), k = 0;
  sort(ps.begin(), ps.end(), [](const P &a, const P &b) {
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  });

  vector<P> ch(2 * n);
  res.resize(2 * n);
  for (int i = 0; i < n; res[k] = i, ch[k++] = ps[i++]) { // lower-hull
    while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      --k;
  }
  for (int i = n - 2, t = k + 1; i >= 0;
       res[k] = i, ch[k++] = ps[i--]) { // upper-hull
    while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      --k;
  }
  ch.resize(k - 1);
  res.resize(k - 1);
  return ch;
}
pair<int, int> convex_diameter(const Polygon &pt) {
  const int n = pt.size();
  int is = 0, js = 0;
  for (int i = 1; i < n; ++i) {
    if (imag(pt[i]) > imag(pt[is]))
      is = i;
    if (imag(pt[i]) < imag(pt[js]))
      js = i;
  }
  number maxd = norm(pt[is] - pt[js]);

  int i, maxi, j, maxj;
  i = maxi = is;
  j = maxj = js;
  do {
    if (cross(diff(pt, i), diff(pt, j)) >= 0)
      j = (j + 1) % n;
    else
      i = (i + 1) % n;
    if (norm(pt[i] - pt[j]) > maxd) {
      maxd = norm(pt[i] - pt[j]);
      maxi = i;
      maxj = j;
    }
  } while (i != is || j != js);
  return make_pair(maxi, maxj); /* farthest pair is (maxi, maxj). */
}

int main() {
  int n;
  while (cin >> n && n > 0) {
    Polygon p(n), q(n);
    REP(i, n) {
      double x, y;
      cin >> x >> y;
      p[i] = {x, y};
    }
    REP(i, n) {
      double x, y;
      cin >> x >> y;
      q[i] = {x, y};
    }
    if (n == 1) {
      printf("%.10f\\n", 0.0);
      continue;
    }
    vector<int> idx;
    auto pc = convex_hull(p, idx);
    auto max_p = convex_diameter(pc);
    int max_i = idx[max_p.first];
    int max_j = idx[max_p.second];
    double max_len = abs(p[max_i] - p[max_j]);
    double ans = 1e64;
    REP(i, n) REP(j, n) if (i != j) {
      double len = abs(q[i] - q[j]);
      if (comp(len, max_len) != 0)
        continue;
      auto qt = q;
      P dif = p[max_i] - qt[i];
      REP(k, qt.size()) { qt[k] += dif; }
      assert(near(p[max_i], qt[i]));
      // double th = angle(p[max_j] - p[max_i], qt[j] - qt[i]);
      // REP(k, qt.size()) {
      //     qt[k] = rotate(qt[k], -th, qt[i]);
      // }
      double th = angle(qt[j] - qt[i], p[max_j] - p[max_i]);
      REP(k, qt.size()) { qt[k] = rotate(qt[k], th, qt[i]); }
      assert(near(p[max_i], qt[i]));
      assert(near(p[max_j], qt[j]));
      if (!equal(p, qt))
        continue;
      th = abs(th);
      ans = min(ans, th);
    }
    printf("%.10f\\n", ans);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;
#define curr(P, i) P[i]
#define next(P, i) P[(i + 1) % P.size()]
#define diff(P, i) (next(P, i) - curr(P, i))
typedef double number;
typedef complex<double> Point;
const double EPS = 1e-8;
typedef Point P;
typedef vector<Point> Polygon;
double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
double angle(P a, P b) { return arg(conj(a) * b); }
P rotate(P a, double b, P c = P()) { return (a - c) * polar(1.0, b) + c; }
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > +EPS)
    return +1; // 反時計回り
  if (cross(b, c) < -EPS)
    return -1; // 時計回り
  if (dot(b, c) < 0)
    return +2; // c--a--b の順番で一直線上
  if (norm(b) < norm(c))
    return -2; // a--b--c の順番で一直線上
  return 0;    // 点が線分ab上にある
}
namespace std {
bool operator<(const P &a, const P &b) {
  return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
}
}; // namespace std
int sign(double x) { return (x > EPS) - (x < -EPS); }
int comp(double x, double y) { return sign(x - y); }
bool near(Point a, Point b) {
  return comp(a.real(), b.real()) == 0 && comp(a.imag(), b.imag()) == 0;
}
bool equal(Polygon p, Polygon q) {
  sort(p.begin(), p.end());
  sort(q.begin(), q.end());
  REP(i, p.size()) {
    if (!near(p[i], q[i]))
      return false;
  }
  return true;
}
Polygon convex_hull(vector<P> ps, vector<int> &res) {
  int n = ps.size(), k = 0;
  sort(ps.begin(), ps.end(), [](const P &a, const P &b) {
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  });

  vector<P> ch(2 * n);
  res.resize(2 * n);
  for (int i = 0; i < n; res[k] = i, ch[k++] = ps[i++]) { // lower-hull
    while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      --k;
  }
  for (int i = n - 2, t = k + 1; i >= 0;
       res[k] = i, ch[k++] = ps[i--]) { // upper-hull
    while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      --k;
  }
  ch.resize(k - 1);
  res.resize(k - 1);
  return ch;
}
pair<int, int> convex_diameter(const Polygon &pt) {
  const int n = pt.size();
  int is = 0, js = 0;
  for (int i = 1; i < n; ++i) {
    if (imag(pt[i]) > imag(pt[is]))
      is = i;
    if (imag(pt[i]) < imag(pt[js]))
      js = i;
  }
  number maxd = norm(pt[is] - pt[js]);

  int i, maxi, j, maxj;
  i = maxi = is;
  j = maxj = js;
  do {
    if (cross(diff(pt, i), diff(pt, j)) >= 0)
      j = (j + 1) % n;
    else
      i = (i + 1) % n;
    if (norm(pt[i] - pt[j]) > maxd) {
      maxd = norm(pt[i] - pt[j]);
      maxi = i;
      maxj = j;
    }
  } while (i != is || j != js);
  return make_pair(maxi, maxj); /* farthest pair is (maxi, maxj). */
}

int main() {
  int n;
  while (cin >> n && n > 0) {
    Polygon p(n), q(n);
    REP(i, n) {
      double x, y;
      cin >> x >> y;
      p[i] = {x, y};
    }
    REP(i, n) {
      double x, y;
      cin >> x >> y;
      q[i] = {x, y};
    }
    vector<int> idx;
    auto pc = convex_hull(p, idx);
    auto max_p = convex_diameter(pc);
    int max_i = idx[max_p.first];
    int max_j = idx[max_p.second];
    double max_len = abs(p[max_i] - p[max_j]);
    double ans = 1e64;
    REP(i, n) REP(j, n) if (i != j) {
      double len = abs(q[i] - q[j]);
      if (comp(len, max_len) != 0)
        continue;
      auto qt = q;
      P dif = p[max_i] - q[i];
      REP(k, qt.size()) { qt[k] += dif; }
      assert(near(p[max_i], qt[i]));
      double th = angle(p[max_j] - p[max_i], qt[j] - qt[i]);
      REP(k, qt.size()) { qt[k] = rotate(qt[k], th, qt[i]); }
      if (!equal(p, qt))
        continue;
      if (sign(th) == 0)
        th = 0;
      if (sign(th) < 0)
        th += M_PI;
      ans = min(ans, th);
    }
    printf("%.12f\\n", ans);
  }
  return 0;
}