Problem p02557 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--)
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); }
template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); }

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\n'
#define dumpV(v)                                                               \
  cout << #v << " : [";                                                        \
  for (auto &t : v)                                                            \
    cout << t << ", ";                                                         \
  cout << "]"                                                                  \
       << " "
#define dumpVL(v)                                                              \
  cout << #v << " : [";                                                        \
  for (auto &t : v)                                                            \
    cout << t << ", ";                                                         \
  cout << "]" << endl
#define STOP assert(false)
#else
#define dump(x)
#define dumpL(x)
#define LINE
#define LINEL
#define dumpV(v)
#define dumpVL(v)
#define STOP assert(false)
#endif
#define mp make_pair

namespace std {
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ", " << a.second << ')';
  return out;
}
} // namespace std

int main() {
  int n;
  cin >> n;
  vi a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  map<int, int> ma, mb;
  rep(i, n) {
    ma[a[i]]++;
    mb[b[i]]++;
  }
  for (auto &&p : mb) {
    if (ma.count(p.first) == 0)
      continue;
    if (p.second + ma[p.first] > n) {
      cout << "No" << '\n';
      return 0;
    }
  }
  using P = pair<int, int>;
  priority_queue<P> pa, pb;
  for (auto &&p : ma)
    pa.emplace(p.second, p.first);
  for (auto &&p : mb)
    pb.emplace(p.second, p.first);
  vector<vector<int>> ind(n + 1);
  rep(i, n) ind[a[i]].emplace_back(i);

  vi ans(n, -1);

  while (pa.size()) {
    auto p = pa.top();
    pa.pop();

    auto q = pb.top();
    pb.pop();
    P x = q;

    if (p.second == q.second) {
      x = pb.top();
      pb.pop();
      pb.emplace(q);
    }

    int a = p.second;
    ans[ind[a].back()] = x.second;
    ind[a].pop_back();
    x.first--;
    p.first--;
    if (x.first > 0)
      pb.emplace(x);
    if (p.first > 0)
      pa.emplace(p);
  }

  // dumpVL(a);
  // dumpVL(ans);

  rep(i, n) if (a[i] == ans[i]) {
    rep(j, n) {
      if (a[i] != ans[j] && a[j] != ans[i]) {
        swap(ans[i], ans[j]);
        break;
      }
    }
  }

  cout << "Yes" << '\n';
  rep(i, n) { cout << ans[i] << (i + 1 == n ? '\n' : ' '); }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--)
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); }
template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); }

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\n'
#define dumpV(v)                                                               \
  cout << #v << " : [";                                                        \
  for (auto &t : v)                                                            \
    cout << t << ", ";                                                         \
  cout << "]"                                                                  \
       << " "
#define dumpVL(v)                                                              \
  cout << #v << " : [";                                                        \
  for (auto &t : v)                                                            \
    cout << t << ", ";                                                         \
  cout << "]" << endl
#define STOP assert(false)
#else
#define dump(x)
#define dumpL(x)
#define LINE
#define LINEL
#define dumpV(v)
#define dumpVL(v)
#define STOP assert(false)
#endif
#define mp make_pair

namespace std {
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ", " << a.second << ')';
  return out;
}
} // namespace std

int main() {
  int n;
  cin >> n;
  vi a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  map<int, int> ma, mb;
  rep(i, n) {
    ma[a[i]]++;
    mb[b[i]]++;
  }
  for (auto &&p : mb) {
    if (ma.count(p.first) == 0)
      continue;
    if (p.second + ma[p.first] > n) {
      cout << "No" << '\n';
      return 0;
    }
  }
  using P = pair<int, int>;
  priority_queue<P> pa, pb;
  for (auto &&p : ma)
    pa.emplace(p.second, p.first);
  for (auto &&p : mb)
    pb.emplace(p.second, p.first);
  vector<vector<int>> ind(n + 1);
  rep(i, n) ind[a[i]].emplace_back(i);

  vi ans(n, -1);

  while (pa.size()) {
    auto p = pa.top();
    pa.pop();

    auto q = pb.top();
    pb.pop();
    P x = q;

    if (p.second == q.second && pb.size() > 0) {
      x = pb.top();
      pb.pop();
      pb.emplace(q);
    }

    int a = p.second;
    ans[ind[a].back()] = x.second;
    ind[a].pop_back();
    x.first--;
    p.first--;
    if (x.first > 0)
      pb.emplace(x);
    if (p.first > 0)
      pa.emplace(p);
  }

  // dumpVL(a);
  // dumpVL(ans);

  rep(i, n) if (a[i] == ans[i]) {
    rep(j, n) {
      if (a[i] != ans[j] && a[j] != ans[i]) {
        swap(ans[i], ans[j]);
        break;
      }
    }
  }

  cout << "Yes" << '\n';
  rep(i, n) { cout << ans[i] << (i + 1 == n ? '\n' : ' '); }

  return 0;
}