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

#include <algorithm>
#include <cstdio>
#include <deque>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

int main() {
  while (const int n = getInt()) {
    const int N = n * (n - 1) / 2;
    vector<int> memo(401);
    vector<int> d(N);
    vector<vector<int>> ans;

    REP(i, N)
    memo[d[i] = getInt()]++;

    sort(d.rbegin(), d.rend());
    if (n == 2) {
      ans.push_back(vector<int>(1, d[0]));
    } else {
      vector<int> tmp(n - 1);
      REP(i, 1 << (n - 2)) {
        int rev = (~i) & ((1 << (n - 2)) - 1);
        if (i > rev)
          continue;

        vector<int> pos;
        pos.reserve(n);
        vector<int> cnt(401);

        pos.push_back(0);
        pos.push_back(d[0]);
        cnt[d[0]]++;

        for (int j = 1; j < N; j++) {
          const int dd = d[j];
          if (cnt[dd] < memo[dd]) {
            int next = pos.size() - 2;
            if (i & (1 << next)) {
              pos.push_back(dd);
            } else {
              pos.push_back(pos[1] - dd);
            }

            const int m = pos.size() - 1;
            REP(k, m) {
              const int p = std::abs(pos[k] - pos[m]);
              if (++cnt[p] > memo[p])
                goto next;
            }
          }
        }

        sort(pos.begin(), pos.end());
        REP(i, n - 1) tmp[i] = pos[i + 1] - pos[i];
        ans.push_back(tmp);
        reverse(tmp.begin(), tmp.end());
        ans.push_back(tmp);
      next:;
      }
    }

    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());

    REP(i, ans.size())
    REP(j, n - 1)
    printf("%d%c", ans[i][j], j == n - 2 ? '\\n' : ' ');

    puts("-----");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <deque>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

int main() {
  while (const int n = getInt()) {
    const int N = n * (n - 1) / 2;
    vector<int> memo(401);
    vector<int> d(N);
    vector<vector<int>> ans;

    REP(i, N)
    memo[d[i] = getInt()]++;

    sort(d.rbegin(), d.rend());
    if (n == 2) {
      ans.push_back(vector<int>(1, d[0]));
    } else {
      REP(i, 1 << (n - 2)) {
        vector<int> pos;
        vector<int> cnt(401);
        vector<int> tmp(n - 1);

        pos.push_back(0);
        pos.push_back(d[0]);
        cnt[d[0]]++;

        for (int j = 1; j < N; j++) {
          const int dd = d[j];
          if (cnt[dd] < memo[dd]) {
            int next = pos.size() - 2;
            if (i & (1 << next)) {
              pos.push_back(dd);
            } else {
              pos.push_back(pos[1] - dd);
            }

            const int m = pos.size() - 1;
            REP(k, m) {
              const int p = std::abs(pos[k] - pos[m]);
              if (++cnt[p] > memo[p])
                goto next;
            }
          }
        }

        sort(pos.begin(), pos.end());
        REP(i, n - 1) tmp[i] = pos[i + 1] - pos[i];
        ans.push_back(tmp);
      next:;
      }
    }

    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());

    REP(i, ans.size())
    REP(j, n - 1)
    printf("%d%c", ans[i][j], j == n - 2 ? '\\n' : ' ');

    puts("-----");
  }
  return 0;
}