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

#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

// const i64 mod = 1'000'000'000ll + 7;
const i64 smod = 998244353ll;

// const i64 inf = mod;

const long double eps = 1e-8;

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);
  cerr.precision(15);
  cerr.setf(ios::fixed);

  i64 n;
  cin >> n;

  vector<i64> a(n);
  vector<i64> b(n);

  for (i64 i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (i64 i = 0; i < n; i++) {
    cin >> b[i];
  }

  map<i64, i64> c;

  for (i64 i = 0; i < n; i++) {
    c[b[i]] += 1;
  }

  map<i64, set<i64>> p;
  for (i64 i = 0; i < n; i++) {
    p[a[i]].insert(i);
  }

  set<pair<i64, i64>, greater<pair<i64, i64>>> q;
  for (auto t : c) {
    q.insert({t.second, t.first});
  }

  for (auto t : c) {
    i64 val = t.first;
    i64 cnt = t.second;

    if (n - p[val].size() < cnt) {
      cout << "No" << endl;
      return 0;
    }
  }

  vector<i64> x;
  for (auto t : q) {
    i64 cnt = t.first;
    i64 val = t.second;
    for (auto i : p[val]) {
      x.push_back(i);
    }
    p.erase(val);
  }

  vector<i64> z(n);
  set<i64> e;

  for (auto t : p) {
    for (auto i : t.second) {
      e.insert(i);
    }
  }

  i64 i = 0;
  vector<i64> y;

  while (!q.empty()) {
    i64 cnt = q.begin()->first;
    i64 val = q.begin()->second;
    q.erase(q.begin());

    set<i64> ne;
    vector<i64> ny;

    while (i < x.size() && a[x[i]] == val) {
      ne.insert(x[i]);
      i += 1;
    }

    while (cnt > 0 && i < x.size()) {
      i64 j = x[i];
      z[j] = val;
      ny.push_back(j);

      i += 1;
      cnt -= 1;
    }

    while (cnt > 0 && !e.empty()) {
      i64 j = *e.begin();
      z[j] = val;
      ny.push_back(j);

      e.erase(j);
      cnt -= 1;
    }

    while (cnt > 0 && !ne.empty() && !y.empty()) {
      i64 j = y.back();

      ny.push_back(j);
      y.pop_back();

      if (a[j] == val) {
        continue;
      }
      i64 k = *ne.begin();

      z[k] = z[j];
      z[j] = val;

      ny.push_back(k);

      ne.erase(k);

      cnt -= 1;
    }

    if (cnt > 0) {
      throw 1;
    }

    for (auto t : ny) {
      y.push_back(t);
    }

    for (auto t : ne) {
      e.insert(t);
    }
  }

  // for (i64 i = 0; i < n; i++) {
  //     if (a[i] == z[i]) {
  //         throw 1;
  //     }
  // }

  cout << "Yes" << endl;
  for (auto t : z) {
    cout << t << " ";
  }

  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

// const i64 mod = 1'000'000'000ll + 7;
const i64 smod = 998244353ll;

// const i64 inf = mod;

const long double eps = 1e-8;

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);
  cerr.precision(15);
  cerr.setf(ios::fixed);

  i64 n;
  cin >> n;

  vector<i64> a(n);
  vector<i64> b(n);

  for (i64 i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (i64 i = 0; i < n; i++) {
    cin >> b[i];
  }

  map<i64, i64> c;

  for (i64 i = 0; i < n; i++) {
    c[b[i]] += 1;
  }

  map<i64, set<i64>> p;
  for (i64 i = 0; i < n; i++) {
    p[a[i]].insert(i);
  }

  set<pair<i64, i64>, greater<pair<i64, i64>>> q;
  for (auto t : c) {
    q.insert({t.second, t.first});
  }

  for (auto t : c) {
    i64 val = t.first;
    i64 cnt = t.second;

    if (n - p[val].size() < cnt) {
      cout << "No" << endl;
      return 0;
    }
  }

  vector<i64> x;
  for (auto t : q) {
    i64 cnt = t.first;
    i64 val = t.second;
    for (auto i : p[val]) {
      x.push_back(i);
    }
    p.erase(val);
  }

  vector<i64> z(n);
  set<i64> e;

  for (auto t : p) {
    for (auto i : t.second) {
      e.insert(i);
    }
  }

  i64 i = 0;
  vector<i64> y;

  while (!q.empty()) {
    i64 cnt = q.begin()->first;
    i64 val = q.begin()->second;
    q.erase(q.begin());

    set<i64> ne;
    vector<i64> ny;

    while (i < x.size() && a[x[i]] == val) {
      ne.insert(x[i]);
      i += 1;
    }

    while (cnt > 0 && i < x.size()) {
      i64 j = x[i];
      z[j] = val;
      ny.push_back(j);

      i += 1;
      cnt -= 1;
    }

    while (cnt > 0 && !e.empty()) {
      i64 j = *e.begin();
      z[j] = val;
      ny.push_back(j);

      e.erase(j);
      cnt -= 1;
    }

    while (cnt > 0 && !ne.empty() && !y.empty()) {
      i64 j = y.back();
      i64 k = *ne.begin();

      z[k] = z[j];
      z[j] = val;

      ny.push_back(j);
      ny.push_back(k);

      y.pop_back();
      ne.erase(k);
    }

    if (cnt > 0) {
      throw 1;
    }

    for (auto t : ny) {
      y.push_back(t);
    }

    for (auto t : ne) {
      e.insert(t);
    }
  }

  // for (i64 i = 0; i < n; i++) {
  //     if (a[i] == z[i]) {
  //         throw 1;
  //     }
  // }

  cout << "Yes" << endl;
  for (auto t : z) {
    cout << t << " ";
  }

  return 0;
}