Problem p02557 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];

  rep(i, N) {
    A[i]--;
    B[i]--;
  }

  vector<ll> an(N, 0), bn(N, 0);
  rep(i, N) an[A[i]]++;
  rep(i, N) bn[B[i]]++;

  rep(i, N) {
    if (min(an[i], bn[i]) > N / 2) {
      cout << "No" << endl;
      return 0;
    }
  }

  vector<int> ord(N, 0);
  rep(i, N) ord[i] = i;
  sort(all(ord), [&](int a, int b) {
    if (an[A[a]] == an[A[b]]) {
      if (bn[A[a]] == bn[A[b]])
        return a < b;
      else
        bn[A[a]] > bn[A[b]];
    } else
      an[A[a]] > an[A[b]];
  });

  vector<int> ans(N, -1);
  vector<int> deg(N, 0);
  rep(i, N) deg[i] = an[i];

  set<tuple<int, int, int>> se;
  rep(i, N) {
    if (bn[i] == 0)
      continue;
    se.insert(mkt(-deg[i], -bn[i], i));
  }

  for (auto i : ord) {
    auto itr = se.begin();
    int ft = get<2>(*itr);
    int tval = A[i];
    if (tval == ft) {
      ++itr;
      int st = get<2>(*itr);
      ans[i] = st;
      se.erase(itr);
      bn[st]--;
      if (bn[st] > 0)
        se.insert(mkt(-deg[st], -bn[st], st));
      if (bn[tval]) {
        se.erase(mkt(-deg[tval], -bn[tval], tval));
        deg[tval]--;
        se.insert(mkt(-deg[tval], -bn[tval], tval));
      }
    } else {
      ans[i] = ft;
      se.erase(itr);
      bn[ft]--;
      if (bn[ft] > 0)
        se.insert(mkt(-deg[ft], -bn[ft], ft));
      if (bn[tval]) {
        se.erase(mkt(-deg[tval], -bn[tval], tval));
        deg[tval]--;
        se.insert(mkt(-deg[tval], -bn[tval], tval));
      }
    }
  }
  cout << "Yes" << endl;

  rep(i, N) cout << ans[i] + 1 << " ";
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];

  rep(i, N) {
    A[i]--;
    B[i]--;
  }

  vector<ll> an(N, 0), bn(N, 0);
  rep(i, N) an[A[i]]++;
  rep(i, N) bn[B[i]]++;

  rep(i, N) {
    if (N - an[i] < bn[i]) {
      cout << "No" << endl;
      return 0;
    }
  }

  vector<int> ord(N, 0);
  rep(i, N) ord[i] = i;
  sort(all(ord), [&](int a, int b) {
    if (an[A[a]] == an[A[b]]) {
      if (bn[A[a]] == bn[A[b]])
        return a < b;
      else
        bn[A[a]] > bn[A[b]];
    } else
      an[A[a]] > an[A[b]];
  });

  vector<int> ans(N, -1);
  vector<int> deg(N, 0);
  rep(i, N) deg[i] = an[i];

  set<tuple<int, int, int>> se;
  rep(i, N) {
    if (bn[i] == 0)
      continue;
    se.insert(mkt(-deg[i], -bn[i], i));
  }

  for (auto i : ord) {
    auto itr = se.begin();
    int ft = get<2>(*itr);
    int tval = A[i];
    if (tval == ft) {
      ++itr;
      int st = get<2>(*itr);
      ans[i] = st;
      se.erase(itr);
      bn[st]--;
      if (bn[st] > 0)
        se.insert(mkt(-deg[st], -bn[st], st));
      if (bn[tval]) {
        se.erase(mkt(-deg[tval], -bn[tval], tval));
        deg[tval]--;
        se.insert(mkt(-deg[tval], -bn[tval], tval));
      }
    } else {
      ans[i] = ft;
      se.erase(itr);
      bn[ft]--;
      if (bn[ft] > 0)
        se.insert(mkt(-deg[ft], -bn[ft], ft));
      if (bn[tval]) {
        se.erase(mkt(-deg[tval], -bn[tval], tval));
        deg[tval]--;
        se.insert(mkt(-deg[tval], -bn[tval], tval));
      }
    }
  }
  cout << "Yes" << endl;

  rep(i, N) cout << ans[i] + 1 << " ";
  cout << endl;

  return 0;
}