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

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

bool solve(const vector<int> &ys, int u, int d, const vector<int> &xs, int l,
           int r, int plusy, int plusx) {
  if (l == r || u == d) {
    if (l == r) {
      return all_of(ys.begin() + u, ys.begin() + d,
                    [&](const int a) { return a == plusy; });

    } else {
      return all_of(xs.begin() + l, xs.begin() + r,
                    [&](const int a) { return a == plusx; });
    }
    return true;
  } else {
    if (xs[l] == plusx) {
      return solve(ys, u, d, xs, l + 1, r, plusy, plusx);
    } else if (xs[l] < plusx) {
      return false;
    }

    if (xs[r - 1] - plusx == d - u) {
      return solve(ys, u, d, xs, l, r - 1, plusy + 1, plusx);
    } else if (xs[r - 1] - plusx > ys.size()) {
      return false;
    }

    if (ys[u] == plusy) {
      return solve(ys, u + 1, d, xs, l, r, plusy, plusx);
    } else if (ys[u] < plusy) {
      return false;
    }

    if (ys[d - 1] - plusy == r - l) {
      return solve(ys, u, d - 1, xs, l, r, plusy, plusx + 1);
    } else if (ys[d - 1] - plusy > xs.size()) {
      return false;
    }

    return false;
  }
}

int main() {

  while (true) {
    int T;
    cin >> T;
    if (!T)
      break;
    vector<int> ys, xs;
    for (int i = 0; i < T; ++i) {
      int a;
      cin >> a;
      ys.push_back(a);
    }
    for (int i = 0; i < T; ++i) {
      int a;
      cin >> a;
      xs.push_back(a);
    }
    sort(ys.begin(), ys.end());
    sort(xs.begin(), xs.end());

    bool ok = solve(ys, 0, T, xs, 0, T, 0, 0);
    if (accumulate(xs.begin(), xs.end(), 0) !=
        accumulate(ys.begin(), ys.end(), 0)) {
      ok = false;
    }
    if (ok) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

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

bool solve(const vector<int> &ys, int u, int d, const vector<int> xs, int l,
           int r, int plusy, int plusx) {
  if (l == r || u == d) {
    return true;
  } else {
    if (xs[l] == plusx) {
      return solve(ys, u, d, xs, l + 1, r, plusy, plusx);
    } else if (xs[l] < plusx) {
      return false;
    }

    if (xs[r - 1] - plusx == d - u) {
      return solve(ys, u, d, xs, l, r - 1, plusy + 1, plusx);
    } else if (xs[r - 1] - plusx > ys.size()) {
      return false;
    }

    if (ys[u] == plusy) {
      return solve(ys, u + 1, d, xs, l, r, plusy, plusx);
    } else if (ys[u] < plusy) {
      return false;
    }

    if (ys[d - 1] - plusy == r - l) {
      return solve(ys, u, d - 1, xs, l, r, plusy, plusx + 1);
    } else if (ys[d - 1] - plusy > xs.size()) {
      return false;
    }

    return false;
  }
}

int main() {

  while (true) {
    int T;
    cin >> T;
    if (!T)
      break;
    vector<int> ys, xs;
    for (int i = 0; i < T; ++i) {
      int a;
      cin >> a;
      ys.push_back(a);
    }
    for (int i = 0; i < T; ++i) {
      int a;
      cin >> a;
      xs.push_back(a);
    }
    sort(ys.begin(), ys.end());
    sort(xs.begin(), xs.end());

    bool ok = solve(ys, 0, T, xs, 0, T, 0, 0);
    if (accumulate(xs.begin(), xs.end(), 0) !=
        accumulate(ys.begin(), ys.end(), 0)) {
      ok = false;
    }
    if (ok) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}