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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

const double EPS = 1e-8;
typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
double cross(const P &a, const P &b) { return imag(conj(a) * b); }
double dot(const P &a, const P &b) { return real(conj(a) * b); }

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
  L() {}
};

typedef vector<P> G;

#define curr(P, i) P[i]
#define next(P, i) P[(i + 1) % P.size()]
struct C {
  P p;
  double r;
  C(const P &p, double r) : p(p), r(r) {}
};

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < 0)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;
}
double area(const G &g) {
  double A = 0;
  for (int i = 0; i < g.size(); ++i) {
    A += cross(g[i], next(g, i));
  }
  return A / 2;
}
P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS)
    return m[0]; // same line
  if (abs(A) < EPS)
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

G convex_cut(const G &g, const L &l) {
  G Q;
  REP(i, g.size()) {
    P A = curr(g, i), B = next(g, i);
    if (ccw(l[0], l[1], A) != -1)
      Q.push_back(A);
    if (ccw(l[0], l[1], A) * ccw(l[0], l[1], B) < 0)
      Q.push_back(crosspoint(L(A, B), l));
  }
  return Q;
}
// Cð½·àÌªlb¤É èACð½·ÅålðßéÆ«BCð½·àÌª³¢Æ«ÌÉÓB
template <class T> T upper_search(T lb, T ub, bool (*C)(T)) {
  REP(i, 100) { // ±±ÍKÉ
    T mid = (lb + ub) / 2;
    if (C(mid))
      lb =
          mid; // Å¬lðßéÈçlbÆubðüêÖ¦é
    else
      ub = mid;
  }
  return lb;
}
const double PI = acos(-1);
P rotate(P p, double ang) { return p * P(cos(ang), sin(ang)); }
G g;
bool C(double x) {
  G hoge(g);
  REP(i, g.size()) {
    L l(g[i], next(g, i));
    P p = rotate(l[1] - l[0], PI / 2);
    p *= x / abs(p);
    l = L(l[0] + p, l[1] + p);
    hoge = convex_cut(hoge, l);
    if (hoge.size() == 0)
      return 0;
  }
  // printf("%.6f", x);
  // cout  << " " << area(hoge) << endl;
  return 1;
}
int main() {
  int n;
  while (cin >> n, n) {
    g.clear();
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      g.push_back(P(a, b));
    }
    double ans = upper_search<double>(0, INF, C);
    printf("%.6f\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
double cross(const P &a, const P &b) { return imag(conj(a) * b); }
double dot(const P &a, const P &b) { return real(conj(a) * b); }

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
  L() {}
};

typedef vector<P> G;

struct C {
  P p;
  double r;
  C(const P &p, double r) : p(p), r(r) {}
};

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < 0)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;
}
P projection(const L &l, const P &p) {
  double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
  return l[0] + t * (l[0] - l[1]);
}
bool intersectSP(const L &s, const P &p) {
  return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) <
         EPS; // triangle inequality
}

double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r))
    return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS)
    return m[0]; // same line
  if (abs(A) < EPS)
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

int main() {
  int n;
  while (cin >> n, n) {
    G g;
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      g.push_back(P(a, b));
    }
    vector<L> ls;
    REP(i, n) {
      P p1, p2;
      if (i == n - 1)
        p1 = g[0];
      else
        p1 = g[i + 1];
      if (i == 0)
        p2 = g[n - 1];
      else
        p2 = g[i - 1];
      p1 -= g[i];
      p2 -= g[i];
      p1 *= 1 / abs(p1);
      p2 *= 1 / abs(p2);
      // cout << l1[0] << " l1 = "  << l1[1] << " " << "l2 = " << l2[1]<< endl;
      L l(g[i], g[i] + p1 + p2);
      ls.push_back(l);
    }
    // REP(i, ls.size()) {
    //   cout << ls[i][0] << " " << ls[i][1] << endl;
    // }
    double ans = 0;
    REP(i, n) {
      REP(j, i) {
        P p = crosspoint(ls[i], ls[j]);
        double mi = INF;
        bool ok = 1;
        REP(k, n) {
          L l;
          if (k == n - 1)
            l = L(g[k], g[0]);
          else
            l = L(g[k], g[k + 1]);
          if (ccw(l[0], l[1], p) != 1) {
            ok = 0;
            break;
          }
          mi = min(mi, distanceSP(l, p));
        }
        if (ok)
          ans = max(ans, mi);
      }
    }
    printf("%.6f\\n", ans);
  }
}