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

#include <bits/stdc++.h>
#define scf scanf
#define ptf printf
#define forp(i, j, k) for (int i = j; i < k; i++)
#define form(i, j, k) for (int i = j; i > k; i--)
#define sz(x) (int)x.size()
#define pb push_back
#define fst first
#define scd second
#define m_p make_pair
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;

const double pi = acos(-1.0);
double R, r;

double sqr(double x) { return x * x; }

double cc(double d) {
  //    double res=0,p,m,x,y,z;
  //    if(d*d+r*r>R*R)
  //    {
  //        p=d;
  //        m=(r*r-R*R)/d;
  //    }
  //    else
  //    {
  //        p=(R*R-r*r)/d;
  //        m=-d;
  //    }
  //    x=(p+m)/2;
  //    y=(p-m)/2;
  //    z=sqrt(r*r-x*x);
  //    res=acos(x/r)*r*r-z*x;
  //    if(d*d+r*r<R*R)
  //        res=pi*r*r-res;
  //    z=sqrt(R*R-y*y);
  //    res+=acos(y/R)*R*R-z*y;
  //    return res;
  if (r + R < d)
    return 0;
  else if (R - r > d)
    return pi * r * r;
  else if (r - R > d)
    return pi * R * R;
  double q1 = acos((r * r + d * d - R * R) / (2 * r * d));
  double q2 = acos((R * R + d * d - r * r) / (2 * R * d));
  return (r * r * q1 + R * R * q2 - r * d * sin(q1));
}

int w, h;

double ok(double x, double y, double r) {
  return x >= r - 1e-4 && x + r <= w + 1e-4 && y >= r - 1e-4 &&
         y + r <= h + 1e-4;
}

int main() {
  int a, b, ab;
  for (; scf("%d%d%d%d%d", &w, &h, &a, &b, &ab);) {
    int fg = 0;
    if (!w && !h && !a && !b && !ab)
      break;
    if (a < b)
      swap(a, b), fg = 1;
    double d;
    R = sqrt(a / pi);
    r = sqrt(b / pi);
    if (ab) {
      double l0 = max(R - r, 1e-7), r0 = R + r, mid, t0;
      forp(ttt, 0, 101) {
        mid = (l0 + r0) / 2;
        //                t0=cc(mid);
        if (cc(mid) < ab)
          r0 = mid;
        else
          l0 = mid;
      }
      d = mid;
    } else
      d = R + r;
    double W, H;
    W = R + r + d;
    H = 2 * R;
    double xA, yA, xB, yB;
    xA = yA = R;
    if (!ok(xA, yA, R)) {
      puts("impossible");
      continue;
    }
    double x0 = w - r, y0 = h - r;
    double dis = sqrt(sqr(xA - x0) + sqr(yA - y0));
    xB = xA + (x0 - xA) * d / dis;
    yB = yA + (y0 - yA) * d / dis;
    if (ok(xB, yB, r)) {
      if (fg) {
        swap(xA, xB);
        swap(yA, yB);
        swap(r, R);
      }
      ptf("%.10f %.10f %.10f %.10f %.10f %.10f\\n", xA, yA, R, xB, yB, r);
    } else
      puts("impossible");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define scf scanf
#define ptf printf
#define forp(i, j, k) for (int i = j; i < k; i++)
#define form(i, j, k) for (int i = j; i > k; i--)
#define sz(x) (int)x.size()
#define pb push_back
#define fst first
#define scd second
#define m_p make_pair
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;

const double pi = acos(-1.0);
double R, r;

double sqr(double x) { return x * x; }

double cc(double d) {
  double res = 0, p, m, x, y, z;
  if (d * d + r * r > R * R) {
    p = d;
    m = (r * r - R * R) / d;
  } else {
    p = (R * R - r * r) / d;
    m = -d;
  }
  x = (p + m) / 2;
  y = (p - m) / 2;
  z = sqrt(r * r - x * x);
  res = acos(x / r) * r * r - z * x;
  if (d * d + r * r < R * R)
    res = pi * r * r - res;
  z = sqrt(R * R - y * y);
  res += acos(y / R) * R * R - z * y;
  return res;
}

int w, h;

double ok(double x, double y, double r) {
  return x >= r - 1e-4 && x + r <= w + 1e-4 && y >= r - 1e-4 &&
         y + r <= h + 1e-4;
}

int main() {
  int a, b, ab;
  for (; scf("%d%d%d%d%d", &w, &h, &a, &b, &ab);) {
    if (!w && !h && !a && !b && !ab)
      break;
    if (a < b)
      swap(a, b);
    double d;
    R = sqrt(a / pi);
    r = sqrt(b / pi);
    if (ab) {
      for (double l0 = max(R - r, 1e-6), r0 = R + r, mid;;) {
        if (fabs(cc(l0) - ab) < 1e-6) {
          d = l0;
          break;
        }
        mid = (l0 + r0) / 2;
        if (cc(mid) < ab)
          r0 = mid;
        else
          l0 = mid;
      }
    } else
      d = R + r;
    double W, H;
    W = R + r + d;
    H = 2 * R;
    double xA, yA, xB, yB;
    xA = yA = R;
    if (!ok(xA, yA, R)) {
      puts("impossible");
      continue;
    }
    double x0 = w - r, y0 = h - r;
    double dis = sqrt(sqr(xA - x0) + sqr(yA - y0));
    xB = xA + (x0 - xA) * d / dis;
    yB = yA + (y0 - yA) * d / dis;
    if (ok(xB, yB, r))
      ptf("%.10f %.10f %.10f %.10f %.10f %.10f\\n", xA, yA, R, xB, yB, r);
    else
      puts("impossible");
  }
  return 0;
}