Problem p02132 (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 FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)

#define CHMIN(a, b) (a) = min((a), (b))
#define CHMAX(a, b) (a) = max((a), (b))

typedef complex<double> P;

int n, m;
P pos[25];

double R[1 << 14];

double f(int msk, double x, double y) {
  P p(x, y);
  double ret = 1e-14;
  REP(i, n) if ((msk >> i) & 1) CHMAX(ret, abs(pos[i] - p));
  return ret;
}

const double ppp = 100.0;
const double qqq = 101.0;

double calc(int msk, double x) {
  double ylow = -1, yhigh = 1e5 + 1;
  REP(_, 40) {
    double ylolo = (qqq * ylow + ppp * yhigh) / (ppp + qqq);
    double yhihi = (ppp * ylow + qqq * yhigh) / (ppp + qqq);
    if (f(msk, x, ylolo) < f(msk, x, yhihi)) {
      yhigh = yhihi;
    } else {
      ylow = ylolo;
    }
  }
  return (yhigh + ylow) / 2.0;
}

double dp[25][1 << 14];

int main() {
  scanf("%d%d", &n, &m);
  REP(i, n) {
    double x, y;
    scanf("%lf%lf", &x, &y);
    pos[i] = P(x, y);
  }
  R[0] = 0;
  FOR(i, 1, 1 << n) {
    double xlow = -1, xhigh = 1e5 + 1;
    REP(_, 40) {
      double xlolo = (qqq * xlow + ppp * xhigh) / (ppp + qqq);
      double xhihi = (ppp * xlow + qqq * xhigh) / (ppp + qqq);
      double ylolo = calc(i, xlolo);
      double yhihi = calc(i, xhihi);
      if (f(i, xlolo, ylolo) < f(i, xhihi, yhihi)) {
        xhigh = xhihi;
      } else {
        xlow = xlolo;
      }
    }
    double x = (xlow + xhigh) / 2.0;
    double y = calc(i, x);
    R[i] = f(i, x, y);
  }

  REP(i, m + 1) REP(msk, 1 << n) dp[i][msk] = 1e18;
  dp[0][0] = 1e-14;
  REP(i, m) REP(msk, 1 << n) {
    int submsk = ((1 << n) - 1) - msk;
    for (int T = submsk;; T = (T - 1) & submsk) {
      CHMIN(dp[i + 1][msk | T], max(dp[i][msk], R[T]));
      if (T == 0)
        break;
    }
  }
  printf("%.9f\\n", dp[m][(1 << n) - 1]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)

#define CHMIN(a, b) (a) = min((a), (b))
#define CHMAX(a, b) (a) = max((a), (b))

typedef complex<double> P;

int n, m;
P pos[25];

double R[1 << 14];

double f(int msk, double x, double y) {
  P p(x, y);
  double ret = 1e-14;
  REP(i, n) if ((msk >> i) & 1) CHMAX(ret, abs(pos[i] - p));
  return ret;
}

double calc(int msk, double x) {
  double ylow = -1, yhigh = 1e5 + 1;
  REP(_, 50) {
    double ylolo = (6.0 * ylow + 5.0 * yhigh) / 11.0;
    double yhihi = (5.0 * ylow + 6.0 * yhigh) / 11.0;
    if (f(msk, x, ylolo) < f(msk, x, yhihi)) {
      yhigh = yhihi;
    } else {
      ylow = ylolo;
    }
  }
  return (yhigh + ylow) / 2.0;
}

double dp[25][1 << 14];

int main() {
  scanf("%d%d", &n, &m);
  REP(i, n) {
    double x, y;
    scanf("%lf%lf", &x, &y);
    pos[i] = P(x, y);
  }
  R[0] = 0;
  FOR(i, 1, 1 << n) {
    double xlow = -1, xhigh = 1e5 + 1;
    REP(_, 50) {
      double xlolo = (6.0 * xlow + 5.0 * xhigh) / 11.0;
      double xhihi = (5.0 * xlow + 6.0 * xhigh) / 11.0;
      double ylolo = calc(i, xlolo);
      double yhihi = calc(i, xhihi);
      if (f(i, xlolo, ylolo) < f(i, xhihi, yhihi)) {
        xhigh = xhihi;
      } else {
        xlow = xlolo;
      }
    }
    double x = (xlow + xhigh) / 2.0;
    double y = calc(i, x);
    R[i] = f(i, x, y);
  }

  REP(i, m + 1) REP(msk, 1 << n) dp[i][msk] = 1e18;
  dp[0][0] = 1e-14;
  REP(i, m) REP(msk, 1 << n) {
    int submsk = ((1 << n) - 1) - msk;
    for (int T = submsk;; T = (T - 1) & submsk) {
      CHMIN(dp[i + 1][msk | T], max(dp[i][msk], R[T]));
      if (T == 0)
        break;
    }
  }
  printf("%.9f\\n", dp[m][(1 << n) - 1]);
  return 0;
}