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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> PI;
const double EPS = 1e-6;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) a.rbegin(), a.rend()
#define FLL(a, b) memset((a), b, sizeof(a))
#define CLR(a) FLL(a, 0)
#define declare(a, it) __typeof(a) it = (a)
#define FOR(it, a) for (declare(a.begin(), it); it != a.end(); ++it)
#define FORR(it, a) for (declare(a.rbegin(), it); it != a.rend(); ++it)
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
  return o << "(" << v.F << ", " << v.S << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  rep(i, SZ(v)) o << (i ? ", " : "") << v[i];
  return o << "}";
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
int s2i(string &a) {
  stringstream ss(a);
  int r;
  ss >> r;
  return r;
}
int geti() {
  int n;
  scanf("%d", &n);
  return n;
}

typedef double FP;

typedef complex<FP> pt;
typedef pt P;
typedef pair<pt, pt> line;
namespace std {
bool operator<(const P &a, const P &b) {
  if (abs(a.real() - b.real()) > EPS)
    return a.real() < b.real();
  return a.imag() < b.imag();
}
} // namespace std
FP dot(P a, P b) { return real(conj(a) * b); }
FP crs(P a, P b) { return imag(conj(a) * b); }
P ortho(P a) { return P(imag(a), -real(a)); }
P ortho(line a) { return ortho(a.second - a.first); }
P crspt(P a, P b, P c, P d) {
  b -= a, d -= c;
  return a + b * crs(d, c - a) / crs(d, b);
}
P crspt(line a, line b) { return crspt(a.first, a.second, b.first, b.second); }
bool onl(P a1, P a2, P b) {
  return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS;
}
bool onl(line a, P b) { return onl(a.first, a.second, b); }
bool iscrs(line a, line b) {
  P c = crspt(a, b);
  return onl(a, c) && onl(b, c);
}
line mkl(P a, P v) { return line(a, a + v); }
FP lpdist(line a, P b) { return abs(b - crspt(a, mkl(b, ortho(a)))); }

void check(P a) {

  // assert(!isnan(a.real()));
  // assert(!isnan(a.imag()));
}

void check(line a) {
  check(a.first);
  check(a.second);
}

bool para(line a, line b) {
  P av = a.first - a.second;
  P bv = b.first - b.second;
  return abs(dot(av / abs(av), bv / abs(bv))) > 1 - 1e-9;
}

double tri(P a, P b, P c) {
  a -= c;
  b -= c;
  return abs(crs(a, b));
}

line midline(line a, line b) {
  if (!para(a, b)) {
    P av = a.second - a.first;
    P bv = b.second - b.first;
    av /= abs(av);
    bv /= abs(bv);
    auto ret = line(crspt(a, b), crspt(a, b) + av + bv);
    check(ret);
    return ret;
  }
  P st = (a.first + b.first) / 2.;
  auto ret = line(st, st + a.second - a.first);
  check(ret);
  return ret;
}

int main(int argc, char *argv[]) {
  int n;
  while (cin >> n && n) {
    int x[n], y[n];
    for (int i = 0; i < n; ++i)
      cin >> x[i] >> y[i];
    double cx = accumulate(x, x + n, 0.) / n;
    double cy = accumulate(y, y + n, 0.) / n;

    double ans = 0;
    for (int i = 0; i < n; ++i) {

      for (int j = 0; j < i; ++j) {
        if (i == j)
          continue;
        double r = 10000000000;
        for (int k = 0; k < n; ++k) {
          if (i == k || j == k)
            continue;
          line il = line(P(x[i], y[i]), P(x[(i + 1) % n], y[(i + 1) % n]));
          line jl = line(P(x[(j + 1) % n], y[(j + 1) % n]), P(x[j], y[j]));
          line kl = line(P(x[(k + 1) % n], y[(k + 1) % n]), P(x[k], y[k]));
          line ij = midline(il, jl);
          line ik = midline(il, kl);
          // if(!iscrs(ij, ik)){
          //   cout << ij << "  " << ik << endl;
          //   continue;
          // }
          P ce = crspt(ij, ik);
          if (!isnormal(ce.real()) || !isnormal(ce.imag())) {
            // cout << ce << endl;
            continue;
          }
          double me = 0;
          double me2 = 0;
          for (int ii = 0; ii < n; ++ii) {
            me += tri(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]), ce);
            me2 += tri(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]),
                       P(cx, cy));
          }

          if (abs(me - me2) / max(me, me2) > EPS) {
            // cout << me << " " << me2 << endl;
            continue;
          }

          double tr = 10000000000;
          for (int ii = 0; ii < n; ++ii) {
            line l(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]));
            tr = min(tr, lpdist(l, ce));
          }
          ans = max(tr, ans);
          // cout << ce << endl;
          double cr = lpdist(il, ce);
          r = min(r, cr);
        }
        // ans = max(r, ans);
      }
    }
    printf("%.8f\\n", ans);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> PI;
const double EPS = 1e-6;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) a.rbegin(), a.rend()
#define FLL(a, b) memset((a), b, sizeof(a))
#define CLR(a) FLL(a, 0)
#define declare(a, it) __typeof(a) it = (a)
#define FOR(it, a) for (declare(a.begin(), it); it != a.end(); ++it)
#define FORR(it, a) for (declare(a.rbegin(), it); it != a.rend(); ++it)
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
  return o << "(" << v.F << ", " << v.S << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  rep(i, SZ(v)) o << (i ? ", " : "") << v[i];
  return o << "}";
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
int s2i(string &a) {
  stringstream ss(a);
  int r;
  ss >> r;
  return r;
}
int geti() {
  int n;
  scanf("%d", &n);
  return n;
}

typedef double FP;

typedef complex<FP> pt;
typedef pt P;
typedef pair<pt, pt> line;
namespace std {
bool operator<(const P &a, const P &b) {
  if (abs(a.real() - b.real()) > EPS)
    return a.real() < b.real();
  return a.imag() < b.imag();
}
} // namespace std
FP dot(P a, P b) { return real(conj(a) * b); }
FP crs(P a, P b) { return imag(conj(a) * b); }
P ortho(P a) { return P(imag(a), -real(a)); }
P ortho(line a) { return ortho(a.second - a.first); }
P crspt(P a, P b, P c, P d) {
  b -= a, d -= c;
  return a + b * crs(d, c - a) / crs(d, b);
}
P crspt(line a, line b) { return crspt(a.first, a.second, b.first, b.second); }
bool onl(P a1, P a2, P b) {
  return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS;
}
bool onl(line a, P b) { return onl(a.first, a.second, b); }
bool iscrs(line a, line b) {
  P c = crspt(a, b);
  return onl(a, c) && onl(b, c);
}
line mkl(P a, P v) { return line(a, a + v); }
FP lpdist(line a, P b) { return abs(b - crspt(a, mkl(b, ortho(a)))); }

void check(P a) {

  // assert(!isnan(a.real()));
  // assert(!isnan(a.imag()));
}

void check(line a) {
  check(a.first);
  check(a.second);
}

bool para(line a, line b) {
  P av = a.first - a.second;
  P bv = b.first - b.second;
  return abs(dot(av / abs(av), bv / abs(bv))) > 1 - 1e-9;
}

double tri(P a, P b, P c) {
  a -= c;
  b -= c;
  return abs(crs(a, b));
}

line midline(line a, line b) {
  if (!para(a, b)) {
    P av = a.second - a.first;
    P bv = b.second - b.first;
    av /= abs(av);
    bv /= abs(bv);
    auto ret = line(crspt(a, b), crspt(a, b) + av + bv);
    check(ret);
    return ret;
  }
  P st = (a.first + b.first) / 2.;
  auto ret = line(st, st + a.second - a.first);
  check(ret);
  return ret;
}

int main(int argc, char *argv[]) {
  int n;
  while (cin >> n && n) {
    int x[n], y[n];
    for (int i = 0; i < n; ++i)
      cin >> x[i] >> y[i];
    double cx = accumulate(x, x + n, 0.) / n;
    double cy = accumulate(y, y + n, 0.) / n;

    double ans = 0;
    for (int i = 0; i < n; ++i) {

      for (int j = 0; j < n; ++j) {
        if (i == j)
          continue;
        double r = 10000000000;
        for (int k = 0; k < n; ++k) {
          if (i == k || j == k)
            continue;
          line il = line(P(x[i], y[i]), P(x[(i + 1) % n], y[(i + 1) % n]));
          line jl = line(P(x[(j + 1) % n], y[(j + 1) % n]), P(x[j], y[j]));
          line kl = line(P(x[(k + 1) % n], y[(k + 1) % n]), P(x[k], y[k]));
          line ij = midline(il, jl);
          line ik = midline(il, kl);
          // if(!iscrs(ij, ik)){
          //   cout << ij << "  " << ik << endl;
          //   continue;
          // }
          P ce = crspt(ij, ik);
          if (!isnormal(ce.real()) || !isnormal(ce.imag())) {
            // cout << ce << endl;
            continue;
          }
          double me = 0;
          double me2 = 0;
          for (int ii = 0; ii < n; ++ii) {
            me += tri(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]), ce);
            me2 += tri(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]),
                       P(cx, cy));
          }

          if (abs(me - me2) / max(me, me2) > EPS) {
            // cout << me << " " << me2 << endl;
            continue;
          }

          double tr = 10000000000;
          for (int ii = 0; ii < n; ++ii) {
            line l(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]));
            tr = min(tr, lpdist(l, ce));
          }
          ans = max(tr, ans);
          // cout << ce << endl;
          double cr = lpdist(il, ce);
          r = min(r, cr);
        }
        // ans = max(r, ans);
      }
    }
    printf("%.8f\\n", ans);
  }
}