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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

bool try_insert(map<int, int> &mp, const vector<int> &anss, int place, int rest,
                int l, int r) {
  vector<int> del_nums;

  if (l == place) {
    int sum = anss[place];
    del_nums.push_back(sum);
    for (int x = l - 1; x >= 0; --x) {
      sum += anss[x];
      del_nums.push_back(sum);
    }

    sum = rest - anss[place];
    del_nums.push_back(sum);
    for (int x = r; x < anss.size(); ++x) {
      sum += anss[x];
      del_nums.push_back(sum);
    }
  } else {
    int sum = rest - anss[place];
    del_nums.push_back(sum);
    for (int x = l - 1; x >= 0; --x) {
      sum += anss[x];
      del_nums.push_back(sum);
    }

    sum = anss[place];
    del_nums.push_back(sum);
    for (int x = r; x < anss.size(); ++x) {
      sum += anss[x];
      del_nums.push_back(sum);
    }
  }
  for (auto del_num : del_nums) {
    auto m = mp.find(del_num);
    if (m == mp.end())
      return false;
    else {
      mp[del_num]--;
      if (mp[del_num] == 0)
        mp.erase(m);
    }
  }
  return true;
}

vector<vector<int>> answers;

void dfs(const vector<int> &anss, int rest, map<int, int> &v, int l, int r) {
  if (l + 1 == r) {
    assert(v.empty());
    {
      auto n_anss(anss);
      n_anss[l] = rest;
      answers.push_back(n_anss);
    }
    return;
  } else {
    int max_num = prev(v.end())->first;
    {
      int l_num = max_num - accumulate(anss.begin(), anss.begin() + l, 0);
      if (l_num <= 0) {
      } else {
        map<int, int> next_v(v);
        auto n_anss(anss);
        n_anss[l] = l_num;
        if (try_insert(next_v, n_anss, l, rest, l, r)) {
          dfs(n_anss, rest - l_num, next_v, l + 1, r);
        }
      }
    }
    {
      int l_num =
          (rest + accumulate(anss.begin() + r, anss.end(), 0)) - max_num;
      if (l_num <= 0) {
      } else {
        map<int, int> next_v(v);
        auto n_anss(anss);
        n_anss[l] = l_num;
        if (try_insert(next_v, n_anss, l, rest, l, r)) {
          dfs(n_anss, rest - l_num, next_v, l + 1, r);
        }
      }
    }
    {
      int r_num = max_num - accumulate(anss.begin() + r, anss.end(), 0);
      if (r_num <= 0) {
      } else {
        map<int, int> next_v(v);
        auto n_anss(anss);
        n_anss[r - 1] = r_num;
        if (try_insert(next_v, n_anss, r - 1, rest, l, r)) {
          dfs(n_anss, rest - r_num, next_v, l, r - 1);
        }
      }
    }
    {
      int r_num =
          (rest + accumulate(anss.begin(), anss.begin() + l, 0)) - max_num;
      if (r_num <= 0) {
      } else {
        map<int, int> next_v(v);
        auto n_anss(anss);
        n_anss[r - 1] = r_num;
        if (try_insert(next_v, n_anss, r - 1, rest, l, r)) {
          dfs(n_anss, rest - r_num, next_v, l, r - 1);
        }
      }
    }
    return;
  }
}

void solve(int N) {
  vector<int> v(N * (N - 1) / 2);
  map<int, int> mp;
  for (int i = 0; i < (N * (N - 1)) / 2; ++i)
    cin >> v[i];
  for (int i = 1; i < (N * (N - 1)) / 2; ++i)
    mp[v[i]]++;

  if (N == 2) {
    answers = (vector<vector<int>>{vector<int>{1}});
  } else {

    vector<int> anss(N - 1, -1);
    dfs(anss, v[0], mp, 0, N - 1);
  }
}

bool operator<(const vector<int> &l, const vector<int> &r) {
  for (int i = 0; i < l.size(); ++i) {
    if (l[i] < r[i])
      return true;
    else if (l[i] > r[i])
      return false;
  }
  return false;
}

int main() {
  int N;
  while (cin >> N) {
    if (!N)
      break;
    answers.clear();
    solve(N);
    sort(answers.begin(), answers.end());
    answers.erase(unique(answers.begin(), answers.end()), answers.end());
    for (auto answer : answers) {
      for (int k = 0; k < N - 1; ++k) {
        cout << answer[k];
        if (k == N - 2)
          cout << endl;
        else
          cout << " ";
      }
    }
    cout << "-----" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

vector<vector<int>> anss;
int N;
void getans(const int total, vector<int> sums, const vector<int> &rests) {
  if (sums.size() == N - 1) {
    anss.push_back(sums);
    return;
  }
  const int a = N - sums.size();
  for (int u = max(0, int(rests.size()) -
                          int(((N - 1) + (N - sums.size())) * sums.size() / 2));
       u < rests.size(); ++u) {
    const int use = rests[u];
    vector<int> nowrest(rests);
    const int num = total - sums[0] - use;
    bool ok = true;
    {
      auto kt = lower_bound(nowrest.begin(), nowrest.end(), use);
      if (kt == nowrest.end() || *kt != use) {
        ok = false;
        continue;
      } else {
        nowrest.erase(kt);
      }
    }
    for (int s = 0; s < sums.size(); ++s) {
      const int a = sums[s] + num;
      {
        auto kt = lower_bound(nowrest.begin(), nowrest.end(), a);
        if (kt == nowrest.end() || *kt != a) {
          ok = false;
          continue;
        } else {
          nowrest.erase(kt);
        }
      }
    }
    if (ok) {
      vector<int> newsums(sums);
      newsums.push_back(0);
      transform(sums.begin(), sums.end(), newsums.begin(),
                [=](const int a) { return a + num; });
      getans(total, newsums, nowrest);
    }
  }
}
int main() {
  while (1) {
    cin >> N;
    anss.clear();
    vector<int> ds;
    for (int i = 0; i < (N) * (N - 1) / 2; ++i) {
      int d;
      cin >> d;
      ds.push_back(d);
    }
    sort(ds.begin(), ds.end());
    int total = ds.back();
    ds.pop_back();
    vector<int> sums;
    sums.push_back(0);
    getans(total, sums, ds);
    set<vector<int>> finanss;
    for (auto a : anss) {
      vector<int> ans;
      for (int i = 0; i < a.size() - 1; ++i) {
        ans.push_back(a[i] - a[i + 1]);
      }
      ans.push_back(total - a[0]);
      finanss.emplace(ans);
    }
    for (auto a : finanss) {
      for (int i = 0; i < a.size(); ++i) {
        cout << a[i];
        if (i != a.size() - 1)
          cout << " ";
      }
      cout << endl;
    }
    cout << "-----" << endl;
  }

  return 0;
}