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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
#define X first
#define Y second
#define rep(i, n) for (int i = 0; i < n; i++)
vector<double> data;
pair<double, double> lc;
vector<bool> done;

double memo[1 << 12][11];

int boxSize;
bool flag;

double solve(double x, double a, double b) {
  return (2.0 * x + sqrt(4 * x * x - 4 * (x * x - 4 * a * b))) / 2.0;
}

void memo_view() {
  for (int i = 0; i < (1 << done.size()); i++) {
    for (int j = 0; j < done.size(); j++) {
      if (i & (1 << j))
        cout << j + 1 << " ";
    }
    cout << endl;
    cout << "=" << memo[i] << endl << endl;
  }
}
void bt() {
  if (count(done.begin(), done.end(), true) == done.size()) {
    if (lc.X + lc.Y <= boxSize) {
      flag = true;
    }
  }
  if (flag || lc.X + lc.Y > boxSize - 1.0e-07)
    return;

  int msk = 0;
  for (int i = 0; i < done.size(); i++) {
    if (done[i])
      msk += 1 << i;
  }
  if (lc.X + lc.Y >= memo[msk][(int)lc.first])
    return;
  memo[msk][(int)lc.first] = lc.X + lc.Y;
  for (int i = 0; i < data.size(); i++) {
    if (done[i])
      continue;

    if (solve(lc.X, lc.Y, data[i]) > boxSize)
      continue;
    done[i] = true;
    pair<double, double> tmp = lc;
    lc = make_pair(solve(lc.X, lc.Y, data[i]), data[i]);
    bt();
    lc = tmp;
    done[i] = false;
  }
}

int main() {
  int t;
  string s;
  while (getline(cin, s) && s != "") {
    vector<int> input;
    flag = false;
    data.clear();

    stringstream ss(s);
    while (ss >> t)
      input.push_back(t);

    boxSize = input[0];
    for (int i = 1; i < input.size(); i++) {
      data.push_back(input[i]);
    }
    sort(data.begin(), data.end(), greater<int>());
    done.resize(data.size());
    fill(done.begin(), done.end(), false);
    rep(i, 1 << 12) rep(j, 11) memo[i][j] = (1 << 12);

    for (int i = 0; i < data.size(); i++) {
      done[i] = true;
      lc = make_pair(data[i], data[i]);
      bt();
      done[i] = false;
    }

    cout << (flag ? "OK" : "NA") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
#define X first
#define Y second

map<double, int> data;
pair<double, double> lc;
vector<int> input;

int boxSize;
bool flag;

double solve(double x, double a, double b) {
  return (2.0 * x + sqrt(4 * x * x - 4 * (x * x - 4 * a * b))) / 2.0;
}

void bt(int depth) {
  if (depth == input.size() - 1) {
    if (lc.X + lc.Y <= boxSize) {
      flag = true;
    }
  }
  if (flag || lc.X + lc.Y > boxSize)
    return;

  for (map<double, int>::iterator it = data.begin(); it != data.end(); it++) {
    if ((*it).second == 0)
      continue;
    // if(solve(lc.X,lc.Y,(*it).first) > boxSize)continue;
    (*it).second--;
    pair<double, double> tmp = lc;
    lc = make_pair(solve(lc.X, lc.Y, (*it).first), (*it).first);
    bt(depth + 1);
    lc = tmp;
    (*it).second++;
  }
}

int main() {
  int t;
  string s;
  while (getline(cin, s) && s != "") {
    flag = false;
    data.clear();
    input.clear();
    stringstream ss(s);
    while (ss >> t)
      input.push_back(t);

    boxSize = input[0];
    for (int i = 1; i < input.size(); i++) {
      data[input[i]]++;
    }

    for (map<double, int>::iterator it = data.begin(); it != data.end(); it++) {
      (*it).second--;
      lc = make_pair((*it).first, (*it).first);
      bt(1);
      (*it).second++;
    }
    cout << (flag ? "OK" : "NA") << endl;
  }
}