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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1)
      out << " ";
  }
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0)
      out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> &mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it))
      out << ", ";
  }
  out << " }";
  return out;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

template <class Type> Type solve(Type res = Type()) {
  int N;
  cin >> N;

  vector<int> A(N), B(N);
  for (auto &in : A)
    cin >> in;
  for (auto &in : B)
    cin >> in;

  int j = 0, pre = -1;
  for (int i = 0; i < N; i++) {
    if (pre != A[i]) {
      j = 0;
    }

    if (A[i] == B[i]) {
      for (; j < N; j++) {
        if (A[j] != A[i] && B[j] != A[i]) {
          swap(B[i], B[j]);
          break;
        }
      }

      if (A[i] == B[i]) {
        cout << "No" << endl;
        return res;
      }
    }
    pre = A[i];
  }

  cout << "Yes" << endl;
  cout << B << endl;

  return res;
}
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  solve(0);
  // cout << fixed << setprecision(15) << solve<ll>() << endl;
  return 0;
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1)
      out << " ";
  }
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0)
      out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> &mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it))
      out << ", ";
  }
  out << " }";
  return out;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

template <class Type> Type solve(Type res = Type()) {
  int N;
  cin >> N;

  vector<int> A(N), B(N);
  vector<int> cntA(N + 1), cntB(N + 1);

  for (auto &in : A)
    cin >> in;
  for (auto &in : B)
    cin >> in;

  for (auto a : A)
    cntA[a]++;
  for (auto b : B)
    cntB[b]++;

  for (int i = 1; i <= N; i++) {
    int min_cnt = min(cntA[i], cntB[i]);

    if (min_cnt > N / 2) {
      cout << "No" << endl;
      return res;
    }
  }
  // cout << "Yes" << endl;

  {
    sort(B.begin(), B.end());
    queue<pii> cand;
    for (int i = N - 1; i >= 0; i--) {
      if (A[i] == B[i]) {
        cand.emplace(B[i], i);
      }
    }

    bool ok = true;
    for (int i = 0; i < N; i++) {
      if (A[i] == B[i]) {
        bool update = false;
        int cnt = (int)cand.size();
        while (cnt--) {
          int val, id;
          tie(val, id) = cand.front();
          cand.pop();
          // cout << i << " : " << A[i] << " " << B[i] << " " <<  val << " " <<
          // id << endl;
          if (val == A[i] || B[i] == A[id]) {
            cand.emplace(val, id);
          } else {
            swap(B[i], B[id]);
            update = true;
            break;
          }
        }
        if (!update) {
          ok = false;
          break;
        }
      }
    }

    if (ok) {
      cout << "Yes" << endl;
      cout << B << endl;
      return res;
    }
  }
  {
    sort(B.rbegin(), B.rend());
    queue<pii> cand;
    for (int i = 0; i < N; i++) {
      if (A[i] == B[i]) {
        cand.emplace(B[i], i);
      }
    }

    bool ok = true;
    for (int i = 0; i < N; i++) {
      if (A[i] == B[i]) {
        bool update = false;
        int cnt = (int)cand.size();
        while (cnt--) {
          int val, id;
          tie(val, id) = cand.front();
          cand.pop();
          // cout << i << " : " << A[i] << " " << B[i] << " " <<  val << " " <<
          // id << endl;
          if (val == A[i] || B[i] == A[id]) {
            cand.emplace(val, id);
          } else {
            swap(B[i], B[id]);
            update = true;
            break;
          }
        }
        if (!update) {
          ok = false;
          break;
        }
      }
    }

    if (ok) {
      cout << "Yes" << endl;
      cout << B << endl;
      return res;
    }
  }

  {
    vector<int> free_B;
    vector<int> tB(N);

    for (int i = 0; i < N; i++) {
      if (cntA[B[i]] == 0) {
        free_B.emplace_back(B[i]);
      }
    }

    priority_queue<pii> pqA, pqB;
    for (int i = 1; i <= N; i++) {
      if (cntA[i] != 0) {
        pqA.emplace(cntA[i], i);
      }
      if (cntB[i] != 0) {
        pqB.emplace(cntB[i], i);
      }
    }

    vector<vector<int>> G(N + 1);
    while (pqB.size()) {
      int cnt, val;
      tie(cnt, val) = pqB.top();
      pqB.pop();

      if (pqA.empty())
        break;
      int cnt1, val1;
      tie(cnt1, val1) = pqA.top();
      pqA.pop();

      if (val != val1) {
        G[val1].emplace_back(val);

        cnt--;
        cnt1--;
        if (cnt) {
          pqB.emplace(cnt, val);
        }
        if (cnt1) {
          pqA.emplace(cnt1, val1);
        }

      } else {
        if (pqA.empty())
          break;

        int cnt2, val2;
        tie(cnt2, val2) = pqA.top();
        pqA.pop();

        pqA.emplace(cnt1, val1);

        if (val != val2) {
          G[val2].emplace_back(val);

          cnt--;
          cnt2--;
          if (cnt) {
            pqB.emplace(cnt, val);
          }
          if (cnt2) {
            pqA.emplace(cnt2, val2);
          }

        } else {
          break;
        }
      }
    }

    if (pqB.empty()) {
      vector<int> tB(N);

      for (int i = 0; i < N; i++) {
        if (G[A[i]].empty()) {
          tB[i] = free_B.back();
          free_B.pop_back();
        } else {
          tB[i] = G[A[i]].back();
          G[A[i]].pop_back();
        }
      }
      return res;
    }
  }

  cout << "No" << endl;
  return res;
}
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  solve(0);
  // cout << fixed << setprecision(15) << solve<ll>() << endl;
  return 0;
}