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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

#define SQR(x) ((x) * (x))

double calcDirec(int r, int l) { return sqrt(SQR(r + l) - SQR(r - l)); }

double memo[12][1 << 12];
int n;

double rec(vint &rs, int prv, int u) {
  if (u + 1 == 1 << n)
    return prv;
  if (prv != -1 && memo[prv][u] != -1)
    return memo[prv][u];

  double res = 1 << 20;

  rep(i, n) if (!(u & 1 << i))
      chmin(res, rec(rs, rs[i], u | 1 << i) +
                     (prv == -1 ? rs[i] : calcDirec(prv, rs[i])));

  return (prv != -1 ? memo[prv][u] = res : res);
}

int main() {
  for (string line; getline(cin, line);) {
    rep(i, 12) rep(j, 1 << 12) memo[i][j] = -1;
    vint rs;

    istringstream iss(line);

    string str;
    iss >> str;

    int H = atoi(str.c_str());

    for (n = 0; iss >> str; n++)
      rs.push_back(atoi(str.c_str()));

    cout << (rec(rs, -1, 0) <= H ? "OK" : "NA") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

vector<string> split(string str, string delim) {
  vector<string> result;
  int cutAt;
  while ((cutAt = str.find_first_of(delim)) != str.npos) {
    if (cutAt > 0) {
      result.push_back(str.substr(0, cutAt));
    }
    str = str.substr(cutAt + 1);
  }
  if (str.length() > 0) {
    result.push_back(str);
  }
  return result;
}

int tonumber(string s) {
  int res = 0;

  rep(i, s.size()) {
    res *= 10;
    res += s[i] - '0';
  }

  return res;
}

bool rec(vint &R, int &n, int &H, double h, int prv, int S) {
  // end
  if (S == (1 << n) - 1) {
    return h + prv <= H;
  }

  bool res = false;

  rep(i, n) {
    if (!(S >> i & 1)) {
      int dr = R[i] + prv;
      int dy = abs(R[i] - prv);
      double dx = sqrt(dr * dr - dy * dy);

      res |= rec(R, n, H, h + dx + (S == 0 ? R[i] : 0), R[i], S | 1 << i);
    }
  }

  return res;
}

int main() {
  string line;
  while (getline(cin, line)) {
    vector<string> ss = split(line, " ");

    int n = ss.size() - 1;
    int H = tonumber(ss[0]);

    vint R(n);

    rep(i, n) R[i] = tonumber(ss[i + 1]);

    cout << (rec(R, n, H, 0, 0, 0) ? "OK" : "NA") << endl;
  }
}