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

#include <algorithm>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
typedef unsigned int fox;
int b[10][2];
struct wolf {
  int s;
  int d;
  int c[8][2];
  wolf() {}
};
inline bool operator<(const wolf &a, const wolf &b) {
  if (a.s + a.d != b.s + b.d)
    return a.s + a.d > b.s + b.d;
  return false;
}
inline int ABS(int a) { return max(a, -a); }
fox mul = 1000000007;
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++) {
      scanf("%d%d", &b[i][0], &b[i][1]);
      if (b[i][0] > b[i][1])
        swap(b[i][0], b[i][1]);
    }
    int n = a;
    priority_queue<wolf> Q;
    wolf st;
    for (int i = 0; i < a; i++) {
      st.c[i][0] = b[i][0];
      st.c[i][1] = b[i][1];
    }
    st.s = 0;
    int sL = 0;
    int sR = 0;
    for (int i = 0; i < a; i++) {
      if (st.c[i][0] - 1 > i)
        sL += st.c[i][0] - 1 - i;
      else
        sR += i - st.c[i][0] + 1;
      if (st.c[i][1] - 1 > i)
        sL += st.c[i][1] - 1 - i;
      else
        sR += i - st.c[i][1] + 1;
    }
    bool big = true;
    for (int i = 0; i < a; i++)
      if (!(st.c[i][0] + i == 8 && st.c[i][1] + i == 8))
        big = false;
    if (big) {
      printf("40\\n");
      continue;
    }
    int jk = sL + sR;
    jk = min(jk, 41);
    st.d = max(sL, sR);
    Q.push(st);
    set<fox> S;
    fox goal = 0;
    for (int i = 0; i < n; i++) {
      goal *= mul;
      goal += i + 1;
      goal *= mul;
      goal += i + 1;
    }
    int ret = 9999999;
    while (Q.size()) {
      if (Q.size() > 100000) {
        priority_queue<wolf> nq;
        for (int i = 0; i < 10000; i++) {
          nq.push(Q.top());
          Q.pop();
        }
        Q = nq;
        continue;
      }
      wolf now = Q.top();
      Q.pop();

      fox key = 0;
      for (int i = 0; i < n; i++) {
        key *= mul;
        key += now.c[i][0];
        key *= mul;
        key += now.c[i][1];
      }
      if (now.s + now.d > jk)
        continue;
      if (key == goal) {
        printf("%d\\n", now.s);
        break;
      }
      if (S.count(key))
        continue;
      S.insert(key);
      for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < 2; j++)
          for (int k = 0; k < 2; k++) {
            if (j && now.c[i][j] == now.c[i][j - 1])
              continue;
            if (k && now.c[i + 1][k] == now.c[i + 1][k - 1])
              continue;
            if (now.c[i][j] <= now.c[i + 1][k])
              continue;
            wolf to = now;
            swap(to.c[i][j], to.c[i + 1][k]);
            to.s = now.s + 1;
            if (to.c[i][0] > to.c[i][1])
              swap(to.c[i][0], to.c[i][1]);
            if (to.c[i + 1][0] > to.c[i + 1][1])
              swap(to.c[i + 1][0], to.c[i + 1][1]);
            to.d = 0;
            int tR = 0;
            int tL = 0;
            for (int l = 0; l < n; l++) {
              if (to.c[l][0] - 1 < l)
                tL += l - to.c[l][0] + 1;
              else
                tR += to.c[l][0] - 1 - l;
              if (to.c[l][1] - 1 < l)
                tL += l - to.c[l][1] + 1;
              else
                tR += to.c[l][1] - 1 - l;
            }
            to.d = max(tR, tL);
            jk = min(jk, tR + tL + to.s);
            // to.d=(to.d+1)/2;
            if (to.s + to.d <= jk)
              Q.push(to);
          }
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
typedef unsigned int fox;
int b[10][2];
struct wolf {
  int s;
  int d;
  int c[8][2];
  wolf() {}
};
inline bool operator<(const wolf &a, const wolf &b) {
  if (a.s + a.d != b.s + b.d)
    return a.s + a.d > b.s + b.d;
  return false;
}
inline int ABS(int a) { return max(a, -a); }
fox mul = 1000000007;
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++) {
      scanf("%d%d", &b[i][0], &b[i][1]);
      if (b[i][0] > b[i][1])
        swap(b[i][0], b[i][1]);
    }
    int n = a;
    priority_queue<wolf> Q;
    wolf st;
    for (int i = 0; i < a; i++) {
      st.c[i][0] = b[i][0];
      st.c[i][1] = b[i][1];
    }
    st.s = 0;
    int sL = 0;
    int sR = 0;
    for (int i = 0; i < a; i++) {
      if (st.c[i][0] - 1 > i)
        sL += st.c[i][0] - 1 - i;
      else
        sR += i - st.c[i][0] + 1;
      if (st.c[i][1] - 1 > i)
        sL += st.c[i][1] - 1 - i;
      else
        sR += i - st.c[i][1] + 1;
    }
    bool big = true;
    for (int i = 0; i < a; i++)
      if (!(st.c[i][0] + i == 8 && st.c[i][1] + i == 8))
        big = false;
    if (big) {
      printf("40\\n");
      continue;
    }
    int jk = sL + sR;
    jk = min(jk, 41);
    st.d = max(sL, sR);
    Q.push(st);
    set<fox> S;
    fox goal = 0;
    for (int i = 0; i < n; i++) {
      goal *= mul;
      goal += i + 1;
      goal *= mul;
      goal += i + 1;
    }
    int ret = 9999999;
    while (Q.size()) {
      if (Q.size() > 1000000) {
        priority_queue<wolf> nq;
        for (int i = 0; i < 100000; i++) {
          nq.push(Q.top());
          Q.pop();
        }
        Q = nq;
        continue;
      }
      wolf now = Q.top();
      Q.pop();

      fox key = 0;
      for (int i = 0; i < n; i++) {
        key *= mul;
        key += now.c[i][0];
        key *= mul;
        key += now.c[i][1];
      }
      if (now.s + now.d > jk)
        continue;
      if (key == goal) {
        printf("%d\\n", now.s);
        break;
      }
      if (S.count(key))
        continue;
      S.insert(key);
      for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < 2; j++)
          for (int k = 0; k < 2; k++) {
            if (j && now.c[i][j] == now.c[i][j - 1])
              continue;
            if (k && now.c[i + 1][k] == now.c[i + 1][k - 1])
              continue;
            if (now.c[i][j] <= now.c[i + 1][k])
              continue;
            wolf to = now;
            swap(to.c[i][j], to.c[i + 1][k]);
            to.s = now.s + 1;
            if (to.c[i][0] > to.c[i][1])
              swap(to.c[i][0], to.c[i][1]);
            if (to.c[i + 1][0] > to.c[i + 1][1])
              swap(to.c[i + 1][0], to.c[i + 1][1]);
            to.d = 0;
            int tR = 0;
            int tL = 0;
            for (int l = 0; l < n; l++) {
              if (to.c[l][0] - 1 < l)
                tL += l - to.c[l][0] + 1;
              else
                tR += to.c[l][0] - 1 - l;
              if (to.c[l][1] - 1 < l)
                tL += l - to.c[l][1] + 1;
              else
                tR += to.c[l][1] - 1 - l;
            }
            to.d = max(tR, tL);
            jk = min(jk, tR + tL + to.s);
            // to.d=(to.d+1)/2;
            if (to.s + to.d <= jk)
              Q.push(to);
          }
      }
    }
  }
}