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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define F first
#define S second
typedef pair<int, int> PI;

int n;

int x[100], y[100], l[100];

pair<double, int> get(double cx, double cy) {
  double maxh = 500;
  int idx = 0;

  rep(i, n) {
    double dx = x[i] - cx;
    double dy = y[i] - cy;
    double dd = sqrt(dx * dx + dy * dy);
    if (maxh > sqrt(max(0., l[i] * l[i] - dd * dd))) {
      maxh = sqrt(max(0., l[i] * l[i] - dd * dd));
      idx = i;
    }
  }
  return mp(maxh, idx);
}

void solve() {
  rep(i, n) cin >> x[i] >> y[i] >> l[i];

  double cx = x[0];
  double cy = y[0];

  double len = 10;
  double ans = 1;
  double tans = 800;

  vector<double> app;

  rep(i, n) rep(j, i) {
    /*
      dd=sqrt(dx*dx+dy*dy);
      sqrt(r1*r1-x*x)+sqrt(r2*r2-x*x)=dd
      sqrt(r1*r1-x*x)=dd-sqrt(r2*r2-x*x)
      r1*r1-x*x=dd^2-2*dd*sqrt(r2*r2-x*x)+r2*r2-x*x
      r1*r1=dd^2-2*dd*sqrt(r2*r2-x*x)+r2*r2
      r1*r1-r2*r2-dd^2=-2*dd*sqrt(r2*r2-x*x)
      (r1*r1-r2*r2-dd^2)^2=4*dd*dd*(r2*r2-x*x);
      (r1*r1-r2*r2-dd^2)^2/4/dd/dd=(r2*r2-x*x);
      x*x=(r2*r2-(r1*r1-r2*r2-dd^2)^2/4/dd/dd);
     */

    double dx = x[j] - x[i];
    double dy = y[j] - y[i];
    double dd = sqrt(dx * dx + dy * dy);
    double xx = sqrt(l[j] * l[j] -
                     pow(l[i] * l[i] - l[j] * l[j] - dd * dd, 2) / 4 / dd / dd);
    // app.pb();
    double r1r = sqrt(l[i] * l[i] - xx * xx);
    /*
      r1c r1r xx    r2c
     */

    double cx = x[i] + dx / dd * r1r;
    double cy = y[i] + dy / dd * r1r;
    ans = max(ans, get(cx, cy).F);
    // if(dd >= l[i]+l[j]) continue;
  }

  // if(n>1) ans=tans;

  while (len > 5e-9) {
    pair<double, int> a = get(cx, cy);
    ans = max(ans, a.F);
    double dx = cx - x[a.S];
    double dy = cy - y[a.S];
    double dd = sqrt(dx * dx + dy * dy);
    cx -= dx * len / dd;
    cy -= dy * len / dd;
    len *= 1 - 2e-4;
  }

  // sort(ALL(app));
  // if(!app.empty() && app[app.size()-1]>=ans){
  //   double p=*lower_bound(ALL(app),ans);
  //   if(ans+2e-4 > p) ans=p;
  // }

  printf("%.8f\\n", ans);
}

int main() {
  while (cin >> n && n)
    solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define F first
#define S second
typedef pair<int, int> PI;

int n;

int x[100], y[100], l[100];

pair<double, int> get(double cx, double cy) {
  double maxh = 500;
  int idx = 0;

  rep(i, n) {
    double dx = x[i] - cx;
    double dy = y[i] - cy;
    double dd = sqrt(dx * dx + dy * dy);
    if (maxh > sqrt(max(0., l[i] * l[i] - dd * dd))) {
      maxh = sqrt(max(0., l[i] * l[i] - dd * dd));
      idx = i;
    }
  }
  return mp(maxh, idx);
}

void solve() {
  rep(i, n) cin >> x[i] >> y[i] >> l[i];

  double cx = x[0];
  double cy = y[0];

  double len = 10;
  double ans = 1;
  while (len > 1e-9) {
    pair<double, int> a = get(cx, cy);
    ans = max(ans, a.F);
    double dx = cx - x[a.S];
    double dy = cy - y[a.S];
    double dd = sqrt(dx * dx + dy * dy);
    cx -= dx * len / dd;
    cy -= dy * len / dd;
    len *= 1 - 2e-4;
  }

  printf("%.8f\\n", ans);
}

int main() {
  while (cin >> n && n)
    solve();
}