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

#include <bits/stdc++.h>
using namespace std;

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

  int n;
  cin >> n;
  // n = 2e5;

  vector<int> a(n), b(n), counta(n + 1), countb(n + 1);
  queue<int> q;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    // a.at(i) = rand() % 3 + 1;
    counta.at(a.at(i))++;
  }
  for (int i = 0; i < n; i++) {
    cin >> b.at(i);
    // b.at(i) = rand() % 3 + 1;
    q.push(b.at(i));
    countb.at(b.at(i))++;
  }

  if (n == 1) {
    if (a[0] == b[0]) {
      cout << "No\\n";
      return 0;
    } else {
      cout << "Yes\\n";
      cout << b[0] << '\\n';
      return 0;
    }
  }

  // sort(a.begin(), a.end());sort(b.begin(), b.end());

  for (int i = 1; i <= n; i++) {
    if (counta.at(i) + countb.at(i) > n) {
      cout << "No\\n";
      return 0;
    }
  }

  priority_queue<pair<int, int>> pq; // counta + countb, number
  for (int i = 1; i <= n; i++) {
    if (countb.at(i) == 0)
      continue;
    pq.push(make_pair(counta[i] + countb[i], i));
  }

  vector<int> ans(n, -1);
  int bad = -1;
  for (int i = 0; i < n; i++) {
    if (countb[a.at(i)] == 0)
      continue;
    auto [pri, num] = pq.top();
    pq.pop();
    if (num != a.at(i)) {
      ans.at(i) = num;
      countb[num]--;
      if (countb[num] > 0)
        pq.push(make_pair(pri, num));
    } else {
      if (pq.empty()) {
        int bad = num;
        ans.at(i) = num;
        countb[num]--;
        if (countb[num] > 0)
          pq.push(make_pair(pri, num));
        continue;
      }
      auto [pri2, num2] = pq.top();
      pq.pop();
      ans.at(i) = num2;
      countb[num2]--;
      if (countb[num2] > 0)
        pq.push(make_pair(pri2, num2));
      pq.push(make_pair(pri, num));
    }
  }

  for (int i = 0; i < n; i++) {
    if (ans.at(i) != -1)
      continue;
    auto [pri, num] = pq.top();
    pq.pop();
    ans.at(i) = num;
    countb[num]--;
    if (countb[num] > 0)
      pq.push(make_pair(pri, num));
  }

  for (int i = 0; i < n; i++) {
    if (ans.at(i) == a.at(i)) {
      while (true) {
        int die = rand() % i;
        swap(ans.at(i), ans.at(die));
        if (ans.at(i) == a.at(i) || ans.at(die) == a.at(die)) {
          swap(ans.at(i), ans.at(die));
        } else {
          break;
        }
      }
    }
  }

  cout << "Yes\\n";
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << ans.at(i);
  }
  cout << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

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

  int n;
  cin >> n;
  // n = 2e5;

  vector<int> a(n), b(n), counta(n + 1), countb(n + 1);
  queue<int> q;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    // a.at(i) = rand() % 3 + 1;
    counta.at(a.at(i))++;
  }
  for (int i = 0; i < n; i++) {
    cin >> b.at(i);
    // b.at(i) = rand() % 3 + 1;
    q.push(b.at(i));
    countb.at(b.at(i))++;
  }
  /*
  if (n == 1) {
    if (a[0] == b[0]) {
      cout << "No\\n";
      return 0;
    }
    else {
      cout << "Yes\\n";
      cout << b[0] << '\\n';
      return 0;
    }
  }*/

  // sort(a.begin(), a.end());sort(b.begin(), b.end());

  for (int i = 1; i <= n; i++) {
    if (counta.at(i) + countb.at(i) > n) {
      cout << "No\\n";
      return 0;
    }
  }

  priority_queue<pair<int, int>> pq; // counta + countb, number
  for (int i = 1; i <= n; i++) {
    if (countb.at(i) == 0)
      continue;
    pq.push(make_pair(counta[i] + countb[i], i));
  }

  vector<int> ans(n, -1);
  for (int i = 0; i < n; i++) {
    if (countb[a.at(i)] == 0)
      continue;
    auto [pri, num] = pq.top();
    pq.pop();
    if (num != a.at(i)) {
      ans.at(i) = num;
      countb[num]--;
      if (countb[num] > 0)
        pq.push(make_pair(pri - 1, num));
    } else {
      auto [pri2, num2] = pq.top();
      pq.pop();
      ans.at(i) = num2;
      countb[num2]--;
      if (countb[num2] > 0)
        pq.push(make_pair(pri2 - 1, num2));
      pq.push(make_pair(pri, num));
    }
  }

  for (int i = 0; i < n; i++) {
    if (ans.at(i) != -1)
      continue;
    auto [pri, num] = pq.top();
    pq.pop();
    ans.at(i) = num;
    countb[num]--;
    if (countb[num] > 0)
      pq.push(make_pair(pri, num));
  }

  cout << "Yes\\n";
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << ans.at(i);
  }
  cout << '\\n';
}