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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
using namespace std;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<double, P> P2;

const double EPS = 1e-7;
P v[3];

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

P rotate(P p, double rad) {
  double x = real(p) * cos(rad) - imag(p) * sin(rad);
  double y = real(p) * sin(rad) + imag(p) * cos(rad);
  return P(x, y);
}

P crossPoint(L l, L m) {
  double A = cross(l.second - l.first, m.second - m.first);
  double B = cross(l.second - l.first, l.second - m.first);
  if (fabs(A) < EPS && fabs(B) < EPS)
    return m.first;
  return m.first + B / A * (m.second - m.first);
}

double areaV() {
  double sum = 0.0;
  for (int i = 0; i < 3; i++)
    sum += (real(v[i]) - real(v[(i + 1) % 3])) *
           (imag(v[i]) + imag(v[(i + 1) % 3]));
  return abs(sum) / 2.0;
}

bool equal(double a, double b) { return fabs(a - b) < EPS; }

P2 check2(P2 p, int pos) {
  double le = 0.1,
         ri = 2.0 * areaV() /
              (abs(v[1] - v[0]) + abs(v[2] - v[1]) + abs(v[0] - v[2]));
  L l[2];
  P2 res;
  for (int i = 0; i < 1000; i++) {
    double mid = (le + ri) / 2.0;

    for (int j = pos; j < pos + 2; j++) {
      P a = (v[(j + 1) % 3] - v[j % 3]) * mid / abs(v[(j + 1) % 3] - v[j % 3]);
      P b = rotate(a, M_PI / 2.0);
      l[j - pos] = L(v[(j + 1) % 3] + b, v[j % 3] + b);
    }
    res = P2(mid, crossPoint(l[0], l[1]));

    if (equal(abs(res.second - p.second), res.first + p.first))
      return res;
    else if (abs(res.second - p.second) < res.first + p.first)
      ri = mid;
    else
      le = mid;
  }
}

int check(double r, int pos) {
  L l[2];
  P2 p[3];
  for (int i = 0; i < 2; i++) {
    P a = (v[i + 1] - v[i]) * r / abs(v[i + 1] - v[i]);
    P b = rotate(a, M_PI / 2.0);
    l[i] = L(v[i + 1] + b, v[i] + b);
  }
  p[0] = P2(r, crossPoint(l[0], l[1]));

  for (int i = 1; i < 3; i++)
    p[i] = check2(p[0], i);

  if (pos == 999 ||
      equal(abs(p[2].second - p[1].second), p[2].first + p[1].first)) {
    printf("%.6f %.6f %.6f\\n", p[2].first, p[0].first, p[1].first);
    return 1;
  } else if (abs(p[2].second - p[1].second) < p[2].first + p[1].first)
    return 2;
  else
    return 0;
}

void solve() {
  double le = 0.1,
         ri = 2.0 * areaV() /
              (abs(v[1] - v[0]) + abs(v[2] - v[1]) + abs(v[0] - v[2]));
  for (int i = 0; i < 1000; i++) {
    double mid = (le + ri) / 2.0;
    int res = check(mid, i);
    if (res == 0)
      ri = mid;
    else if (res == 2)
      le = mid;
    else
      return;
  }
}

int main() {
  int x[3], y[3];
  while (cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2] &&
         (x[0] | y[0] | x[1] | y[1] | x[2] | y[2])) {
    for (int i = 0; i < 3; i++)
      v[i] = P((double)x[i], (double)y[i]);
    solve();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
using namespace std;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<double, P> P2;

const double EPS = 1e-6;
P v[3];

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

P rotate(P p, double rad) {
  double x = real(p) * cos(rad) - imag(p) * sin(rad);
  double y = real(p) * sin(rad) + imag(p) * cos(rad);
  return P(x, y);
}

P crossPoint(L l, L m) {
  double A = cross(l.second - l.first, m.second - m.first);
  double B = cross(l.second - l.first, l.second - m.first);
  if (fabs(A) < EPS && fabs(B) < EPS)
    return m.first;
  return m.first + B / A * (m.second - m.first);
}

double areaV() {
  double sum = 0.0;
  for (int i = 0; i < 3; i++)
    sum += (real(v[i]) - real(v[(i + 1) % 3])) *
           (imag(v[i]) + imag(v[(i + 1) % 3]));
  return abs(sum) / 2.0;
}

bool equal(double a, double b) { return fabs(a - b) < EPS; }

P2 check2(P2 p, int pos) {
  double le = 0.1,
         ri = 2.0 * areaV() /
              (abs(v[1] - v[0]) + abs(v[2] - v[1]) + abs(v[0] - v[2]));
  L l[2];
  P2 res;
  for (int i = 0; i < 1000; i++) {
    double mid = (le + ri) / 2.0;

    for (int j = pos; j < pos + 2; j++) {
      P a = (v[(j + 1) % 3] - v[j % 3]) * mid / abs(v[(j + 1) % 3] - v[j % 3]);
      P b = rotate(a, M_PI / 2.0);
      l[j - pos] = L(v[(j + 1) % 3] + b, v[j % 3] + b);
    }
    res = P2(mid, crossPoint(l[0], l[1]));

    if (equal(abs(res.second - p.second), res.first + p.first))
      return res;
    else if (abs(res.second - p.second) < res.first + p.first)
      ri = mid;
    else
      le = mid;
  }
}

int check(double r) {
  L l[2];
  P2 p[3];
  for (int i = 0; i < 2; i++) {
    P a = (v[i + 1] - v[i]) * r / abs(v[i + 1] - v[i]);
    P b = rotate(a, M_PI / 2.0);
    l[i] = L(v[i + 1] + b, v[i] + b);
  }
  p[0] = P2(r, crossPoint(l[0], l[1]));

  for (int i = 1; i < 3; i++)
    p[i] = check2(p[0], i);

  if (equal(abs(p[2].second - p[1].second), p[2].first + p[1].first)) {
    printf("%.6f %.6f %.6f\\n", p[2].first, p[0].first, p[1].first);
    return 1;
  } else if (abs(p[2].second - p[1].second) < p[2].first + p[1].first)
    return 2;
  else
    return 0;
}

void solve() {
  double le = 0.1,
         ri = 2.0 * areaV() /
              (abs(v[1] - v[0]) + abs(v[2] - v[1]) + abs(v[0] - v[2]));
  while (1) {
    double mid = (le + ri) / 2.0;
    int res = check(mid);
    if (res == 0)
      ri = mid;
    else if (res == 2)
      le = mid;
    else
      return;
  }
}

int main() {
  int x[3], y[3];
  while (cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2] &&
         (x[0] | y[0] | x[1] | y[1] | x[2] | y[2])) {
    for (int i = 0; i < 3; i++)
      v[i] = P((double)x[i], (double)y[i]);
    solve();
  }
}