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

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define each(e, c) for (auto &e : c)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); ++it)

double const eps = 1e-8;
double const inf = 1e10;
double const pi = acos(-1);

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

inline double cross(const P &a, const P &b) { return imag(conj(a) * b); }

inline 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() {}
};

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]);
}

inline double distanceLP(const L &l, const P &p) {
  return abs(p - projection(l, p));
}

int n;
P ps[128];
L es[128];

P innerCenter(P a, P b, P c) {
  double la = abs(b - c), lb = abs(c - a), lc = abs(a - b);
  return (la * a + lb * b + lc * c) / (la + lb + lc);
}

enum { OUT, ON, IN };
int convex_contains(const P &p) {
  P g = (ps[0] + ps[n / 3] + ps[2 * n / 3]) / 3.0;
  int a = 0, b = n;
  while (a + 1 < b) {
    int c = (a + b) / 2;
    if (cross(ps[a] - g, ps[c] - g) > 0) {
      if (cross(ps[a] - g, p - g) > 0 && cross(ps[c] - g, p - g) < 0)
        b = c;
      else
        a = c;
    } else {
      if (cross(ps[a] - g, p - g) < 0 && cross(ps[c] - g, p - g) > 0)
        a = c;
      else
        b = c;
    }
  }
  b %= n;
  if (cross(ps[a] - p, ps[b] - p) < 0)
    return 0;
  if (cross(ps[a] - p, ps[b] - p) > 0)
    return 2;
  return 1;
}

double solve() {
  rotate(ps, ps + rand() % n, ps + n);
  ps[n] = ps[0];
  rep(i, n) { es[i] = {ps[i], ps[i + 1]}; }
  P p = innerCenter(ps[0], ps[n / 3], ps[n / 3 * 2]);

  if (n == 3) {
    return distanceLP(es[0], p);
  }
  double mx = inf, Mx = -inf;
  double my = inf, My = -inf;
  rep(i, n) {
    mx = min(mx, ps[i].real());
    my = min(my, ps[i].imag());
    Mx = max(Mx, ps[i].real());
    My = max(My, ps[i].imag());
  }
  double const r[] = {10, 1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7};
  double static d[128];
  int near = 0;
  rep(t, 90000) {
    rep(i, n) { d[i] = distanceLP(es[i], p); }
    near = 0;
    rep(i, n) if (d[i] < d[near]) { near = i; }
    P h = projection(es[near], p);
    P np = p - (h - p) / d[near] * r[t / 10000];
    if (convex_contains(np) == IN) {
      swap(p, np);
    }
  }
  return d[near];
}

int main() {
  srand(time(0));
  while (cin >> n && n) {
    rep(i, n) {
      double x, y;
      cin >> x >> y;
      ps[i] = P(x, y);
    }
    printf("%lf\\n", solve());
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define each(e, c) for (auto &e : c)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); ++it)

double const eps = 1e-8;
double const inf = 1e10;
double const pi = acos(-1);

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() {}
};

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]);
}

double distanceLP(const L &l, const P &p) { return abs(p - projection(l, 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)
    throw;
  return m[0] + B / A * (m[1] - m[0]);
}

int n;
P ps[128];
L es[128];

P innerCenter(P a, P b, P c) {
  double la = abs(b - c), lb = abs(c - a), lc = abs(a - b);
  return (la * a + lb * b + lc * c) / (la + lb + lc);
}

double solve() {
  int v[]{rand() % (n - 2), rand() % (n - 2), rand() % (n - 2)};
  sort(v, v + 3);
  v[1]++;
  v[2] += 2;
  P p = innerCenter(ps[v[0]], ps[v[1]], ps[v[2]]);
  if (n == 3) {
    return distanceLP(es[0], p);
  }
  static double d[128];
  int near = 0;
  double r[] = {1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6};
  rep(t, 700000) {
    rep(i, n) { d[i] = distanceLP(es[i], p); }
    near = 0;
    rep(i, n) {
      if (d[i] < d[near]) {
        near = i;
      }
    }
    P h = projection(es[near], p);
    p -= (h - p) / d[near] * r[t / 100000];
  }

  return d[near];
}

int main() {
  srand(time(0));
  while (cin >> n && n) {
    rep(i, n) {
      double x, y;
      cin >> x >> y;
      ps[i] = P(x, y);
    }
    ps[n] = ps[0];
    rep(i, n) { es[i] = {ps[i], ps[i + 1]}; }
    printf("%lf\\n", solve());
  }
}