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

// 52
#include <cmath>
#include <complex>
#include <iostream>

using namespace std;

typedef complex<double> P;

double cr(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }

int main() {
  for (int n; cin >> n, n;) {
    double x[50], y[50];
    double r[50], m[50];
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i] >> r[i] >> m[i];
    }
    int mc = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        double xx = x[j] - x[i];
        double yy = y[j] - y[i];
        for (int k = 0; k < 2; k++) {
          double r1 = k ? r[i] + m[i] : r[i];
          for (int l = 0; l < 2; l++) {
            double r2 = l ? r[j] + m[j] : r[j];
            P p[2];
            for (int s = -1; s <= 1; s += 2) {
              for (int rs = -1; rs <= 1; rs += 2) {
                double rts = r2 * rs;
                double is =
                    sqrt(xx * xx + yy * yy - (r1 + rts) * (r1 + rts) + 1e-9);
                double xf =
                    r1 * (xx * (r1 + rts) + s * yy * is) / (xx * xx + yy * yy);
                double yf =
                    r1 * (yy * (r1 + rts) - s * xx * is) / (xx * xx + yy * yy);
                for (int v = 0; v < 2; v++) {
                  if (fabs(yf) > fabs(xf) || 1) {
                    p[v] = P(v + x[i], (r1 * r1 - xf * v) / yf + y[i]);
                  } else {
                    p[v] = P((r1 * r1 - yf * v) / xf + x[i], v + y[i]);
                  }
                }
                int c = 0;
                for (int i = 0; i < n; i++) {
                  P jw = P(x[i], y[i]);
                  double ds =
                      fabs(cr(p[1] - p[0], jw - p[0])) / abs(p[1] - p[0]);
                  /*
                  cout<<xx*xx+yy*yy-(r1+r2)*(r1+r2)<<' '<<xf<<' '<<yf<<' ';
                  cout<<i<<':'<<r[i]<<' '<<ds<<' '<<r[i]+m[i]<<endl;
                  */
                  c += r[i] - 1e-5 < ds && ds < r[i] + m[i] + 1e-5;
                }
                if (mc < c) {
                  mc = c;
                }
              }
            }
          }
        }
      }
    }
    cout << (n > 1 ? mc : 1) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// 52
#include <cmath>
#include <complex>
#include <iostream>

using namespace std;

typedef complex<double> P;

double cr(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }

int main() {
  for (int n; cin >> n, n;) {
    double x[50], y[50];
    double r[50], m[50];
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i] >> r[i] >> m[i];
    }
    int mc = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        double xx = x[j] - x[i];
        double yy = y[j] - y[i];
        for (int k = 0; k < 2; k++) {
          double r1 = k ? r[i] + m[i] : r[i];
          for (int l = 0; l < 2; l++) {
            double r2 = l ? r[j] + m[j] : r[j];
            P p[2];
            for (int s = -1; s <= 1; s += 2) {
              for (int rs = -1; rs <= 1; rs += 2) {
                double is = sqrt(xx * xx + yy * yy - (r1 + r2) * (r1 + r2));
                double xf = r1 * (xx * (r1 + r2 * rs) + s * yy * is) /
                            (xx * xx + yy * yy);
                double yf = r1 * (yy * (r1 + r2 * rs) - s * xx * is) /
                            (xx * xx + yy * yy);
                for (int v = 0; v < 2; v++) {
                  if (fabs(yf) > fabs(xf)) {
                    p[v] = P(v + x[i], (r1 * r1 - xf * v) / yf + y[i]);
                  } else {
                    p[v] = P((r1 * r1 - yf * v) / xf + x[i], v);
                  }
                }
                int c = 0;
                for (int i = 0; i < n; i++) {
                  P jw = P(x[i], y[i]);
                  double ds =
                      fabs(cr(p[1] - p[0], jw - p[0])) / abs(p[1] - p[0]);
                  cout << xx * xx + yy * yy - (r1 + r2) * (r1 + r2) << ' ' << xf
                       << ' ' << yf << ' ';
                  cout << i << ':' << r[i] << ' ' << ds << ' ' << r[i] + m[i]
                       << endl;
                  c += r[i] - 1e-5 < ds && ds < r[i] + m[i] + 1e-5;
                }
                if (mc < c) {
                  mc = c;
                }
              }
            }
          }
        }
      }
    }
    cout << (n > 1 ? mc : 1) << endl;
  }
  return 0;
}