Problem p01276 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, vector<P>> S;
void output(int time, S s) {
  printf("time %d, inv %d\\n", time, s.first);
  for (auto vi : s.second)
    printf("(%d, %d) ", vi.first, vi.second);
  cout << endl;
}
int main() {
  int n;
  while (cin >> n && n) {
    vector<P> v(n);
    for (auto &vi : v)
      cin >> vi.first >> vi.second;
    for (auto &vi : v)
      if (vi.first < vi.second)
        swap(vi.first, vi.second);
    int inv = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (v[i].first > v[j].first)
          inv++;
        if (v[i].first > v[j].second)
          inv++;
        if (v[i].second > v[j].first)
          inv++;
        if (v[i].second > v[j].second)
          inv++;
      }
    }
    priority_queue<S, vector<S>, greater<S>> que;
    set<vector<P>> used;
    que.push(S(inv, v));
    used.insert(v);
    const int BEAM = 1 << 10;
    for (int iter = 0;; iter++) {
      priority_queue<S, vector<S>, greater<S>> nque;
      if (que.top().first == 0) {
        cout << iter << endl;
        break;
      }
      for (int i = 0; i < BEAM && !que.empty(); i++) {
        S s = que.top();
        que.pop();
        int inv = s.first;
        vector<P> &v = s.second;
        for (int i = 0; i < n - 1; i++) {
          if (v[i].first > v[i + 1].second) {
            vector<P> nv = v;
            int ninv = inv - 1;
            swap(nv[i].first, nv[i + 1].second);
            if (nv[i].first < nv[i].second) {
              swap(nv[i].first, nv[i].second);
              ninv--;
            }
            if (nv[i + 1].first < nv[i + 1].second) {
              swap(nv[i + 1].first, nv[i + 1].second);
              ninv--;
            }
            if (!used.count(nv)) {
              used.insert(nv);
              nque.push(S(ninv, nv));
            }
          }
        }
      }
      que.swap(nque);
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, vector<P>> S;
void output(int time, S s) {
  printf("time %d, inv %d\\n", time, s.first);
  for (auto vi : s.second)
    printf("(%d, %d) ", vi.first, vi.second);
  cout << endl;
}
int main() {
  int n;
  while (cin >> n && n) {
    vector<P> v(n);
    for (auto &vi : v)
      cin >> vi.first >> vi.second;
    for (auto &vi : v)
      if (vi.first < vi.second)
        swap(vi.first, vi.second);
    int inv = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (v[i].first > v[j].first)
          inv++;
        if (v[i].first > v[j].second)
          inv++;
        if (v[i].second > v[j].first)
          inv++;
        if (v[i].second > v[j].second)
          inv++;
      }
    }
    priority_queue<S, vector<S>, greater<S>> que;
    que.push(S(inv, v));
    const int BEAM = 1 << 16;
    for (int iter = 0;; iter++) {
      priority_queue<S, vector<S>, greater<S>> nque;
      if (que.top().first == 0) {
        cout << iter << endl;
        break;
      }
      for (int i = 0; i < BEAM && !que.empty(); i++) {
        S s = que.top();
        que.pop();
        int inv = s.first;
        vector<P> &v = s.second;
        for (int i = 0; i < n - 1; i++) {
          if (v[i].first > v[i + 1].second) {
            vector<P> nv = v;
            int ninv = inv - 1;
            swap(nv[i].first, nv[i + 1].second);
            if (nv[i].first < nv[i].second) {
              swap(nv[i].first, nv[i].second);
              ninv--;
            }
            if (nv[i + 1].first < nv[i + 1].second) {
              swap(nv[i + 1].first, nv[i + 1].second);
              ninv--;
            }
            nque.push(S(ninv, nv));
          }
        }
      }
      que.swap(nque);
    }
  }
  return 0;
}