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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\\n")
#define priln(n) printf("%d\\n", n)
#define lpriln(n) printf("%lld\\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
#define linf LLONG_MAX
#define lninf LLONG_MIN
const ll mod = 1e9 + 7;
const int N = 2e5 + 4;

int main() {
  int n;
  scn(n);

  int a[n], b[n];

  vector<int> fa(n + 1, 0), fb(n + 1, 0);

  rep(i, 0, n) scn(a[i]), fa[a[i]]++;

  rep(i, 0, n) scn(b[i]), fb[b[i]]++;

  rep(i, 1, n + 1) if (fa[i] + fb[i] > n) {
    printf("No\\n");
    return 0;
  }

  printf("Yes\\n");

  reverse(b, b + n);

  int cnt = 0, val = -1, st = -1;

  rep(i, 0, n) if (a[i] == b[i]) {
    cnt++, val = a[i];

    if (st == -1)
      st = i;
  }

  // cout<<cnt<<endl;

  // rep(i,0,n)
  // cout<<a[i]<<" "; cout<<endl;

  // rep(i,0,n)
  // cout<<b[i]<<" "; cout<<endl;

  if (cnt) {
    vector<int> v;

    rep(i, 0, n) if (a[i] != b[i] and a[i] != val and b[i] != val) v.pb(i);

    assert((int)v.size() >= cnt);

    rep(i, 0, cnt) swap(b[v[i]], b[st++]);
  }

  rep(i, 0, n) assert(a[i] != b[i]);

  rep(i, 0, n) printf("%d ", b[i]);

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\\n")
#define priln(n) printf("%d\\n", n)
#define lpriln(n) printf("%lld\\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
#define linf LLONG_MAX
#define lninf LLONG_MIN
const ll mod = 1e9 + 7;
const int N = 1e5 + 4;

int main() {
  int n;
  scn(n);

  vector<int> fa(n + 1, 0), fb(n + 1, 0);

  int a[n], b[n];

  vector<int> v[n + 1];

  rep(i, 0, n) {
    scn(a[i]), fa[a[i]]++;

    v[a[i]].pb(i);
  }

  multiset<int> ms, garbage;

  rep(i, 0, n) {
    scn(b[i]), fb[b[i]]++;

    ms.insert(b[i]);
    garbage.insert(b[i]);
  }

  rep(i, 1, n + 1) if (fa[i] + fb[i] > n) {
    printf("No");
    return 0;
  }

  printf("Yes\\n");

  vector<int> ans(n, -1);

  vector<int> va, vb;

  int culprit = 0, mx = 0;

  rep(i, 1, n + 1) {
    int cnt = min(fa[i], fb[i]);

    if (cnt > mx)
      mx = cnt, culprit = i;

    while (cnt--)
      va.pb(i), garbage.erase(garbage.find(i));
  }

  sort(va.begin(), va.end());

  int len = (int)va.size();
  vb.resize(len + 2);

  if (2 * mx > len) {
    while (2 * mx > len) {
      int ind = v[culprit].back();
      v[culprit].pop_back();

      assert(!garbage.empty());

      ans[ind] = *garbage.begin();
      garbage.erase(garbage.begin());

      ms.erase(ms.find(ans[ind]));

      mx--;
      len--;
    }

    vector<int> tv;

    while (mx--)
      tv.pb(culprit);

    rep(i, 0, len) if (va[i] != culprit) tv.pb(va[i]);

    va = tv;
  }

  len = (int)va.size();

  rep(i, 0, len) {
    int ni = (i + len / 2) % len;

    vb[ni] = va[i];
  }

  rep(i, 0, len) assert(va[i] != vb[i]);

  rep(i, 0, len) {
    int it = va[i];

    int ind = v[it].back();
    v[it].pop_back();

    ans[ind] = vb[i];
    ms.erase(ms.find(ans[ind]));
  }

  rep(i, 0, n) assert(ans[i] != a[i]);

  rep(i, 0, n) if (ans[i] == -1) {
    assert(!ms.empty());

    ans[i] = *ms.begin();
    ms.erase(ms.begin());
  }

  rep(i, 0, n) assert(ans[i] != a[i]);

  rep(i, 0, n) printf("%d ", ans[i]);

  return 0;
}