Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>

using namespace std;
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  a = abs(a), b = abs(b);
  while (b > 0) {
    tie(a, b) = make_pair(b, a % b);
  }
  return a;
}
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
// constexpr long long mod = 1000000007LL;
constexpr long long mod = 998244353;
constexpr int MAX = 5000000;

int main() {

  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> cnt(n + 1);
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i], cnt[a[i]]++;
  vector<int> b(n);
  for (int i = 0; i < n; i++)
    cin >> b[i], cnt[b[i]]++;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  }

  reverse(b.begin(), b.end());
  int lf = 0;
  int rg = n - 1;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      if (a[lf] != b[i] and a[i] != b[lf]) {
        swap(b[lf], b[i]);
        lf++;
      } else {
        swap(b[i], b[rg]);
        rg--;
      }
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 0; i < n; i++)
    cout << b[i] << " \\n"[i + 1 == n];
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>

using namespace std;
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  a = abs(a), b = abs(b);
  while (b > 0) {
    tie(a, b) = make_pair(b, a % b);
  }
  return a;
}
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
constexpr int MAX = 5000000;

int main() {
  /*
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  */

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i], a[i]--;
  vector<int> b(n);
  for (int i = 0; i < n; i++)
    cin >> b[i], b[i]--;
  {
    vector<int> ans;
    bool t = true;
    for (int i = 0; i < n; i++) {
      if (a[i] != b[i]) {
        ans.emplace_back(b[i]);
      } else {
        reverse(b.begin() + i, b.end());
        if (a[i] == b[i]) {
          t = false;
          break;
        }
        ans.emplace_back(b[i]);
      }
    }
    if (t) {
      cout << "Yes"
           << "\\n";
      for (int i = 0; i < n; i++) {
        cout << ans[i] + 1 << " \\n"[i + 1 == n];
      }
      return 0;
    }
  }
  {
    reverse(b.begin(), b.end());
    vector<int> ans;
    bool t = true;
    for (int i = 0; i < n; i++) {
      if (a[i] != b[i]) {
        ans.emplace_back(b[i]);
      } else {
        reverse(b.begin() + i, b.end());
        if (a[i] == b[i]) {
          t = false;
          break;
        }
        ans.emplace_back(b[i]);
      }
    }
    if (t) {
      cout << "Yes"
           << "\\n";
      for (int i = 0; i < n; i++) {
        cout << ans[i] + 1 << " \\n"[i + 1 == n];
      }
      return 0;
    }
  }
  vector<int> cnt(n);
  for (int i = 0; i < n; i++)
    cnt[a[i]]++;
  map<pair<int, int>, int> mpa, mpb;
  map<int, vector<int>> res;
  for (int i = 0; i < n; i++) {
    mpb[{-cnt[b[i]], b[i]}]++;
  }
  for (int i = 0; i < n; i++) {
    mpa[{-cnt[a[i]], a[i]}]++;
  }
  bool t = true;
  for (auto itr = mpa.begin(); itr != mpa.end();) {
    auto &p = itr->first;
    int t = p.second;
    int s = itr->second;
    bool f = false;
    int x, y, z;

    while (!mpb.empty() and s > 0) {
      auto it = mpb.begin();
      if (it->first.second == t) {
        f = true;
        x = itr->first.first;
        y = itr->first.second;
        z = itr->second;
        mpb.erase(mpb.begin());
        continue;
      }
      if (s > it->second) {
        s -= it->second;

        for (int i = 0; i < it->second; i++) {
          res[t].emplace_back(it->first.second);
        }
        mpb.erase(it);
      } else {
        for (int i = 0; i < s; i++) {
          res[t].emplace_back(it->first.second);
        }
        it->second -= s;
        s = 0;
      }
    }
    if (s > 0) {
      t = false;
      break;
    }
    if (f) {
      mpb[{0, y}] = z;
    } else {
      auto it = mpb.lower_bound({-cnt[t], t});
      if (!(it == mpb.end() or it->first != make_pair(-cnt[t], t))) {
        int x = it->first.second;
        int y = it->second;
        mpb.erase(it);
        mpb[{0, x}] = y;
      }
    }
    itr = mpa.erase(itr);
  }
  if (t) {
    cout << "Yes"
         << "\\n";
    for (int i = 0; i < n; i++) {
      cout << res[a[i]].back() + 1 << " \\n"[i + 1 == n];
      res[a[i]].pop_back();
    }
  } else {
    cout << "No"
         << "\\n";
  }
  return 0;
}