Problem p00862 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

using ld = long double;
const ld eps = 1e-10, pi = acos(-1.0);

bool eq(ld a, ld b) { return abs(a - b) < eps; }

using Point = complex<ld>;
class Line {
public:
  Point a, b;
  Line() {}
  Line(Point a, Point b) : a(a), b(b) {}
};

ld dot(Point a, Point b) { return real(conj(a) * b); }

ld cross(Point a, Point b) { return imag(conj(a) * b); }

int ccw(Point a, Point b, Point c) {
  b -= a, c -= a;
  if (cross(b, c) > eps)
    return 1; // a,b,c??????????¨???¨???
  if (cross(b, c) < -eps)
    return -1; // a,b,c???????¨???????
  if (dot(b, c) < 0)
    return 2; // c,a,b????????§??´???
  if (norm(b) < norm(c))
    return -2; // a,b,c???????????´???
  return 0;    // a,c,b???????????´???
}

bool isis_ll(Line l, Line m) { return abs(cross(l.b - l.a, m.b - m.a)) > eps; }

bool isis_ls(Line l, Line s) {
  return (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}

bool isis_lp(Line l, Point p) { return abs(cross(l.b - p, l.a - p)) < eps; }

bool isis_sp(Line s, Point p) {
  return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps;
}

Point proj(Line l, Point p) {
  ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
  return l.a + t * (l.a - l.b);
}

Point is_ll(Line s, Line t) {
  Point sv = s.b - s.a, tv = t.b - t.a;
  assert(cross(sv, tv) != 0);
  return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}

bool isis_ss(Line s, Line t) {
  if (isis_ll(s, t))
    return isis_ls(s, t) && isis_ls(t, s);
  return isis_sp(s, t.a) || isis_sp(s, t.b) || isis_sp(t, s.a);
}

ld dist_lp(Line l, Point p) { return abs(p - proj(l, p)); }

ld dist_ll(Line l, Line m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); }

ld dist_ls(Line l, Line s) {
  return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}

ld dist_sp(Line s, Point p) {
  Point r = proj(s, p);
  return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}

ld dist_ss(Line s, Line t) {
  if (isis_ss(s, t))
    return 0;
  return min(
      {dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});
}

Line bi_deg(Point a, Point b, Point c) {
  Point d = (b - c) / (a - c);
  ld deg = atan2l(d.imag(), d.real()) / 2.0;
  return Line(c, c + (Point(cosl(deg), sinl(deg)) * (a - c)));
}

Point H(Line a, Line b, Line c) {
  if (!isis_ll(a, b)) {
    Point x = is_ll(b, c), y = is_ll(c, a);
    return is_ll(bi_deg(b.a, c.b, x), bi_deg(c.a, a.b, y));
  } else if (!isis_ll(b, c)) {
    Point x = is_ll(c, a), y = is_ll(a, b);
    return is_ll(bi_deg(c.a, a.b, x), bi_deg(a.a, b.b, y));
  } else {
    Point x = is_ll(a, b), y = is_ll(b, c);
    return is_ll(bi_deg(a.a, b.b, x), bi_deg(b.a, c.b, y));
  }
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(7);
  int n;
  while (cin >> n, n) {
    vector<Point> p(n);
    for (int i = 0, x, y; i < n; i++) {
      cin >> x >> y;
      p[i] = Point(x, y);
    }
    vector<Line> l(n);
    for (int i = 0; i < n; i++) {
      l[i] = bi_deg(p[i], p[(i + 2) % n], p[(i + 1) % n]);
    }
    vector<Line> e(n);
    for (int i = 0; i < n; i++) {
      e[i] = Line(p[i], i + 1 < n ? p[i + 1] : p[0]);
    }
    vector<Point> all;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        for (int k = j + 1; k < n; k++) {
          if (isis_ll(e[i], e[j]) || isis_ll(e[j], e[k]) ||
              isis_ll(e[k], e[i])) {
            all.push_back(H(e[i], e[j], e[k]));
          }
        }
      }
    }
    ld res = 0;
    for (auto is : all) {
      bool flag = false;
      for (int k = 0; k < n; k++) {
        if (ccw(p[k], k + 1 < n ? p[k + 1] : p[k + 1 - n], is) != 1) {
          flag = true;
          break;
        }
      }
      if (flag) {
        continue;
      }
      ld mi = 1e10;
      for (int k = 0; k < n; k++) {
        mi = min(mi, dist_lp(e[k], is));
      }
      res = max(res, mi);
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ld = long double;
const ld eps = 1e-8, pi = acos(-1.0);

bool eq(ld a, ld b) { return abs(a - b) < eps; }

using Point = complex<ld>;
class Line {
public:
  Point a, b;
  Line() {}
  Line(Point a, Point b) : a(a), b(b) {}
};

ld dot(Point a, Point b) { return real(conj(a) * b); }

ld cross(Point a, Point b) { return imag(conj(a) * b); }

int ccw(Point a, Point b, Point c) {
  b -= a, c -= a;
  if (cross(b, c) > eps)
    return 1; // a,b,c??????????¨???¨???
  if (cross(b, c) < -eps)
    return -1; // a,b,c???????¨???????
  if (dot(b, c) < 0)
    return 2; // c,a,b????????§??´???
  if (norm(b) < norm(c))
    return -2; // a,b,c???????????´???
  return 0;    // a,c,b???????????´???
}

bool isis_ll(Line l, Line m) { return abs(cross(l.b - l.a, m.b - m.a)) > eps; }

bool isis_ls(Line l, Line s) {
  return (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}

bool isis_lp(Line l, Point p) { return abs(cross(l.b - p, l.a - p)) < eps; }

bool isis_sp(Line s, Point p) {
  return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps;
}

Point proj(Line l, Point p) {
  ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
  return l.a + t * (l.a - l.b);
}

Point is_ll(Line s, Line t) {
  Point sv = s.b - s.a, tv = t.b - t.a;
  assert(cross(sv, tv) != 0);
  return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}

bool isis_ss(Line s, Line t) {
  if (isis_ll(s, t))
    return isis_ls(s, t) && isis_ls(t, s);
  return isis_sp(s, t.a) || isis_sp(s, t.b) || isis_sp(t, s.a);
}

ld dist_lp(Line l, Point p) { return abs(p - proj(l, p)); }

ld dist_ll(Line l, Line m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); }

ld dist_ls(Line l, Line s) {
  return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}

ld dist_sp(Line s, Point p) {
  Point r = proj(s, p);
  return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}

ld dist_ss(Line s, Line t) {
  if (isis_ss(s, t))
    return 0;
  return min(
      {dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});
}

Line bi_deg(Point a, Point b, Point c) {
  Point d = (b - c) / (a - c);
  ld deg = atan2l(d.imag(), d.real()) / 2.0;
  return Line(c, c + (Point(cosl(deg), sinl(deg)) * (a - c)));
}

Point H(Line a, Line b, Line c) {
  if (!isis_ll(a, b)) {
    Point x = is_ll(b, c), y = is_ll(c, a);
    return is_ll(bi_deg(b.a, a.b, x), bi_deg(c.a, a.b, y));
  } else if (!isis_ll(b, c)) {
    Point x = is_ll(c, a), y = is_ll(a, b);
    return is_ll(bi_deg(c.a, a.b, x), bi_deg(a.a, b.b, y));
  } else {
    Point x = is_ll(a, b), y = is_ll(b, c);
    return is_ll(bi_deg(a.a, b.b, x), bi_deg(b.a, c.b, y));
  }
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(7);
  int n;
  while (cin >> n, n) {
    vector<Point> p(n);
    for (int i = 0, x, y; i < n; i++) {
      cin >> x >> y;
      p[i] = Point(x, y);
    }
    vector<Line> l(n);
    for (int i = 0; i < n; i++) {
      l[i] = bi_deg(p[i], p[(i + 2) % n], p[(i + 1) % n]);
    }
    vector<Line> e(n);
    for (int i = 0; i < n; i++) {
      e[i] = Line(p[i], i + 1 < n ? p[i + 1] : p[0]);
    }
    vector<Point> all;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        for (int k = j + 1; k < n; k++) {
          if (isis_ll(e[i], e[j]) || isis_ll(e[j], e[k]) ||
              isis_ll(e[k], e[i])) {
            all.push_back(H(e[i], e[j], e[k]));
          }
        }
      }
    }
    ld res = 0;
    for (auto is : all) {
      ld deg = 0;
      for (int k = 0; k < n; k++) {
        Point tmp = ((k + 1 >= n ? p[k + 1 - n] : p[k + 1]) - is) / (p[k] - is);
        deg += atan2l(tmp.imag(), tmp.real());
      }
      if (eq(deg, 0)) {
        continue;
      }
      ld mi = 1e10;
      for (int k = 0; k < n; k++) {
        mi = min(mi, dist_lp(e[k], is));
      }
      res = max(res, mi);
    }
    cout << res << endl;
  }
  return 0;
}