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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

#define MAX 12

using namespace std;
long double minimumLength;
long double boxLength;

long double CalculateRoleCakesLength(const vector<int> &r) {
  long double ret;
  ret = r[0] + r[r.size() - 1];
  for (unsigned int i = 0; i < r.size() - 1; ++i) {
    ret += 2 * sqrt((long double)(r[i] * r[i + 1]));
  }
  return ret;
}
inline void InternalInsert(vector<int> &v, unsigned int a, unsigned int dest) {
  if (a > dest) {
    v.insert(v.begin() + dest, v[a]);
    v.erase(v.begin() + a + 1);
  } else if (dest > a) {
    v.insert(v.begin() + dest, v[a]);
    v.erase(v.begin() + a);
  }
}
template <typename T> void printVector(vector<T> &v) {
  for (unsigned int i = 0; i < v.size(); ++i)
    cout << v[i] << ' ';
  cout << endl;
}
long double OptimizeArrangeOfRoleCakes(const long double target,
                                       vector<int> &vInBoxCakes) {
  long double len;
  long double minlen;
  const long double nowlen = CalculateRoleCakesLength(vInBoxCakes);
  vector<pair<int, int>> vCands;

  minlen = target;
  for (unsigned int i = 0; i < vInBoxCakes.size(); ++i) {
    for (unsigned int j = 0; j <= vInBoxCakes.size(); ++j) {
      if (j >= i + 2 || j <= i - 1) {
        InternalInsert(vInBoxCakes, i, j);
        len = CalculateRoleCakesLength(vInBoxCakes);
        if (len < minlen) {
          minlen = len;
          vCands.push_back(make_pair(i, j));
        }
        if (i < j)
          InternalInsert(vInBoxCakes, j - 1, i);
        else
          InternalInsert(vInBoxCakes, j, i + 1);
      }
    }
  }
  if (vCands.size() > 0) {
    long double retminlen = nowlen;
    int retminlenind = -1;

    for (unsigned int i = 0; i < vCands.size(); ++i) {
      long double t;
      InternalInsert(vInBoxCakes, vCands[i].first, vCands[i].second);

      t = OptimizeArrangeOfRoleCakes(minlen, vInBoxCakes);
      if (t < retminlen) {
        retminlen = t;
        minimumLength = min(minimumLength, retminlen);
        retminlenind = i;
      }

      if (vCands[i].first < vCands[i].second) {
        InternalInsert(vInBoxCakes, vCands[i].second - 1, vCands[i].first);
      } else {
        InternalInsert(vInBoxCakes, vCands[i].second, vCands[i].first + 1);
      }
    }
    return retminlen;
  }
  return nowlen;
}
int main(void) {
  while (true) {

    long double length = 0;
    vector<int> vInBoxCakes;

    cin >> boxLength;
    if (cin.eof())
      break;
    while (true) {
      int tmp;

      if ('\\n' == cin.get())
        break;
      cin >> tmp;
      vInBoxCakes.push_back(tmp);
    }

    minimumLength = CalculateRoleCakesLength(vInBoxCakes);
    OptimizeArrangeOfRoleCakes(minimumLength, vInBoxCakes);

    // cout << "Minmum Length = " << minimumLength << endl;
    if (minimumLength <= boxLength)
      cout << "OK\\n";
    else
      cout << "NA\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>

#define MAX 12

using namespace std;

typedef unsigned int set;

typedef struct tagcombstruct {
  set K, N, x, overx;
} combStruct;

int g_cnt;

/* gencomb1 from http://www5.airnet.ne.jp/tomy/cpro/pe1.htm (C) Tomy */
set nextset(set x) {
  set smallest, ripple, new_smallest, ones;

  smallest = x & (~x + 1);
  ripple = x + smallest;
  new_smallest = ripple & (~ripple + 1);
  ones = ((new_smallest / smallest) >> 1) - 1;

  return ripple | ones;
}
bool subcomb(set p[], combStruct &cmst) {
  set tj, *tq;
  set ts;

  cmst.x = nextset(cmst.x);

  if (cmst.x & cmst.overx)
    return false;
  for (tj = 1, tq = p, ts = cmst.x; tj <= cmst.N; tj++) {
    if (ts & 1)
      *tq++ = tj;
    ts >>= 1;
  }
  return true;
}
vector<pair<int, int>> myunique(const vector<int> &v) {
  vector<pair<int, int>> vret;
  unsigned int now;

  for (unsigned int i = 0; i < v.size(); i++)
    vret.push_back(make_pair(v[i], 0));
  now = 0;
  while (now < vret.size()) {
    int cnt = 1;
    for (unsigned int i = now + 1; i < vret.size(); ++i) {
      if (vret[i].first != vret[i - 1].first)
        break;
      else
        cnt++;
    }
    for (unsigned int i = now; i < now + cnt; ++i)
      vret[i].second = cnt;
    now += cnt;
  }
  while (true) {
    vector<pair<int, int>>::iterator itV = unique(vret.begin(), vret.end());
    if (itV != vret.end())
      vret.erase(itV, vret.end());
    else
      break;
  }
  return vret;
}
bool Solve3(pair<int, bool> *box, set num, set combn, vector<pair<int, int>> &v,
            int pos, long double w) {
  if (pos == v.size()) {
    /*g_cnt++;
    if( g_cnt % 5000000 == 0 ){
            cout << g_cnt << endl;
    }*/
    /*
    long double ret;
    ret = box[0].first + box[num - 1].first;
    for(unsigned int i = 0; i < num - 1; ++i){
            ret += 2 * sqrt((long double)(box[i].first * box[i+1].first));
    }
    if( ret <= w ){
            //cout << ret << endl;
            return true;
    }*/
  } else {
    combStruct cmst;
    set p[MAX];
    set j, *q;
    set s;
    // int ePlacesNum = 0;
    // int eplaces[MAX];

    /*for(unsigned int i = 0; i < num; i++) if( !box[i].second ){
            eplaces[ePlacesNum] = i;
            ePlacesNum++;
    }*/
    cmst.N = combn;
    cmst.K = v[pos].second;
    cmst.x = (1 << cmst.K) - 1L;
    cmst.overx = ~((1 << cmst.N) - 1L);

    for (j = 1, q = p, s = cmst.x; j <= cmst.N; j++) {
      if (s & 1)
        *q++ = j;
      s >>= 1;
    }
    do {
      // TODO
      /*for(int i = 0; i < cmst.K; i++){
              cout << p[i] << ' ';
      }
      cout << endl;*/
      // cout << '(' << cmst.K << ',' << vEmptyPlaces.size() << ")\\n";
      /*for(unsigned int i = 0; i < cmst.K; ++i){
              box[ eplaces[p[i] - 1] ] = make_pair( v[pos].first, true );
      }*/

      if (Solve3(box, num, combn - cmst.K, v, pos + 1, w))
        return true;

      /*for(unsigned int i = 0; i < cmst.K; ++i){
              box[ eplaces[p[i] - 1] ] = make_pair( 0, false );
      }*/
    } while (subcomb(p, cmst));
  }
  return false;
}
int main(void) {
  while (true) {
    int num = 0;
    long double boxLength;
    pair<int, bool> box[MAX];
    vector<pair<int, int>> vRCakes;
    vector<int> vCakes;

    cin >> boxLength;
    if (cin.eof())
      break;
    while (true) {
      int tmp;

      if ('\\n' == cin.get())
        break;
      cin >> tmp;
      vCakes.push_back(tmp);
      box[num].first = 0;
      box[num].second = false;
      num++;
    }

    sort(vCakes.begin(), vCakes.end());

    // unique
    vRCakes = myunique(vCakes);

    g_cnt = 0;
    if (Solve3(box, num, num, vRCakes, 0, boxLength))
      cout << "OK\\n";
    else
      cout << "NA\\n";
    // cout << g_cnt << endl;
  }
  return 0;
}