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

#pragma GCC optimize("O2")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
using namespace std;

#define pb push_back
#define ff first
#define ss second
#define endl '\\n'
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);

/////////////////////////////////////////////

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

///////////////////////////////
#define int long long

const int mod = 1e9 + 7;

void solve() {

  int n;
  cin >> n;
  int a[n], b[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int j = 0; j < n; ++j)
    cin >> b[j];

  map<int, int> fa, fb;
  for (int i = 0; i < n; ++i) {

    fa[a[i]]++;
    fb[b[i]]++;
  }

  for (int i = 0; i < n; ++i) {

    if (fb[b[i]] + fa[b[i]] > n) {

      cout << "No\\n";
      return;
    }
  }

  vector<int> v[n + 1];

  set<int> avail;
  for (int i = 0; i < n; ++i) {

    if (a[i] != b[i])
      continue;
    else {

      if (!avail.empty()) {

        if (b[*avail.begin()] == b[i]) {

          avail.insert(i);
        } else {

          swap(b[*avail.begin()], b[i]);
          avail.erase(avail.begin());
        }
      } else
        avail.insert(i);
    }
  }
  int left = 0;
  while (!avail.empty()) {

    while (b[left] == b[*avail.begin()])
      ++left;
    swap(b[*avail.begin()], b[left++]);
    avail.erase(avail.begin());
  }
  cout << "Yes\\n";
  for (int i = 0; i < n; ++i) {

    if (b[i] == a[i]) {

      int j = i;
      while (j < n && a[i] == b[j]) {

        ++j;
      }
      if (j < n)
        swap(b[i], b[j]);
      j = i;
      while (j >= 0 && a[i] == b[j]) {

        --j;
      }
      if (j >= 0)
        swap(b[i], b[j]);
    }
  }
  for (int i = 0; i < n; ++i)
    cout << b[i] << " ";
  cout << endl;
  // vector<int> ans(n , 0);
  // for(int i = 1 ; i <= n ; ++i){

  // 	vector<int> temp;
  // 	for(auto &idx : v[i]){

  // 		if(avail.find(idx)!=avail.end())
  // 			temp.push_back(idx) , avail.erase(idx);
  // 	}
  // 	for(auto &idx : v[i]){

  // 		ans[*(--avail.end())] = b[idx];
  // 		avail.erase(--avail.end());
  // 	}

  // 	for(auto &idx : temp)
  // 		avail.insert(idx);

  // }
  // for(int i = 0 ; i < n ; ++i){

  // 	if(ans[i]==0)
  // 		ans[i] = b[i];
  // }
  // cout<<"Yes\\n";
  // for(int &val : ans)
  // 	cout<<val<<" ";
  // cout<<endl;
}

signed main() {

  fast;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}


Error: WA

Fixed submission:

#pragma GCC optimize("O2")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
using namespace std;

#define pb push_back
#define ff first
#define ss second
#define endl '\\n'
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);

/////////////////////////////////////////////

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

///////////////////////////////
#define int long long

const int mod = 1e9 + 7;

void solve() {

  int n;
  cin >> n;
  int a[n], b[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int j = 0; j < n; ++j)
    cin >> b[j];

  map<int, int> fa, fb;
  for (int i = 0; i < n; ++i) {

    fa[a[i]]++;
    fb[b[i]]++;
  }

  for (int i = 0; i < n; ++i) {

    if (fb[b[i]] + fa[b[i]] > n) {

      cout << "No\\n";
      return;
    }
  }

  vector<int> v[n + 1];

  set<int> avail;
  for (int i = 0; i < n; ++i) {

    if (a[i] != b[i])
      continue;
    else {

      if (!avail.empty()) {

        if (b[*avail.begin()] == b[i]) {

          avail.insert(i);
        } else {

          swap(b[*avail.begin()], b[i]);
          avail.erase(avail.begin());
        }
      } else
        avail.insert(i);
    }
  }
  int left = 0;
  while (!avail.empty()) {

    while (b[left] == b[*avail.begin()])
      ++left;
    swap(b[*avail.begin()], b[left++]);
    avail.erase(avail.begin());
  }
  cout << "Yes\\n";
  for (int i = 0; i < n; ++i) {
    cout << b[i] << " ";
    assert(b[i] != a[i]);
  }
  cout << endl;
  // vector<int> ans(n , 0);
  // for(int i = 1 ; i <= n ; ++i){

  // 	vector<int> temp;
  // 	for(auto &idx : v[i]){

  // 		if(avail.find(idx)!=avail.end())
  // 			temp.push_back(idx) , avail.erase(idx);
  // 	}
  // 	for(auto &idx : v[i]){

  // 		ans[*(--avail.end())] = b[idx];
  // 		avail.erase(--avail.end());
  // 	}

  // 	for(auto &idx : temp)
  // 		avail.insert(idx);

  // }
  // for(int i = 0 ; i < n ; ++i){

  // 	if(ans[i]==0)
  // 		ans[i] = b[i];
  // }
  // cout<<"Yes\\n";
  // for(int &val : ans)
  // 	cout<<val<<" ";
  // cout<<endl;
}

signed main() {

  fast;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}