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

/* #region Head */

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using vll = vc<ll>;
using vvll = vvc<ll>;
using vld = vc<ld>;
using vvld = vvc<ld>;
using vs = vc<string>;
using vvs = vvc<string>;
template <class T, class U> using um = unordered_map<T, U>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqa = priority_queue<T, vc<T>, greater<T>>;
template <class T> using us = unordered_set<T>;

#define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i))
#define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i))
#define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i))
#define REPD(i, m, n, d)                                                       \\
  for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d))
#define REPMD(i, m, n, d)                                                      \\
  for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d))
#define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) begin(x), end(x)
#define SIZE(x) ((ll)(x).size())
#define PERM(c)                                                                \\
  sort(ALL(c));                                                                \\
  for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define UNIQ(v) v.erase(unique(ALL(v)), v.end());

#define endl '\\n'
#define sqrt sqrtl
#define floor floorl
#define log2 log2l

constexpr ll INF = 1'010'000'000'000'000'017LL;
constexpr int IINF = 1'000'000'007LL;
constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.14159265358979323846;

template <typename T>
istream &operator>>(istream &is, vc<T> &vec) { // vector 入力
  for (T &x : vec)
    is >> x;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vc<T> &vec) { // vector 出力 (for dump)
  os << "{";
  REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "" : ", ");
  os << "}";
  return os;
}
template <typename T>
ostream &operator>>(ostream &os, vc<T> &vec) { // vector 出力 (inline)
  REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "\\n" : " ");
  return os;
}

template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &pair_var) { // pair 入力
  is >> pair_var.first >> pair_var.second;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) { // pair 出力
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}

// map, um, set, us 出力
template <class T> ostream &out_iter(ostream &os, T &map_var) {
  os << "{";
  REPI(itr, map_var) {
    os << *itr;
    auto itrcp = itr;
    if (++itrcp != map_var.end())
      os << ", ";
  }
  return os << "}";
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  return out_iter(os, map_var);
}
template <typename T, typename U>
ostream &operator<<(ostream &os, um<T, U> &map_var) {
  os << "{";
  REPI(itr, map_var) {
    auto [key, value] = *itr;
    os << "(" << key << ", " << value << ")";
    auto itrcp = itr;
    if (++itrcp != map_var.end())
      os << ", ";
  }
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
  return out_iter(os, set_var);
}
template <typename T> ostream &operator<<(ostream &os, us<T> &set_var) {
  return out_iter(os, set_var);
}
template <typename T> ostream &operator<<(ostream &os, pq<T> &pq_var) {
  pq<T> pq_cp(pq_var);
  os << "{";
  if (!pq_cp.empty()) {
    os << pq_cp.top(), pq_cp.pop();
    while (!pq_cp.empty())
      os << ", " << pq_cp.top(), pq_cp.pop();
  }
  return os << "}";
}

// dump
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) > 0)
    DUMPOUT << ", ";
  dump_func(move(tail)...);
}

// chmax (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}

// chmin (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

// ローカル用
#define DEBUG_

#ifdef DEBUG_
#define DEB
#define dump(...)                                                              \\
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                              \\
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl  \\
          << "    ",                                                           \\
      dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

#define VAR(type, ...)                                                         \\
  type __VA_ARGS__;                                                            \\
  cin >> __VA_ARGS__;

template <typename T> istream &operator,(istream &is, T &rhs) {
  return is >> rhs;
}
template <typename T> ostream &operator,(ostream &os, const T &rhs) {
  return os << rhs;
}

struct AtCoderInitialize {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;
  AtCoderInitialize() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} ATCODER_INITIALIZE;

void Yn(bool p) { cout << (p ? "Yes" : "No") << endl; }
void YN(bool p) { cout << (p ? "YES" : "NO") << endl; }

/* #endregion */

template <typename T> vc<pair<T, ll>> vec_continuous_cnts(vc<T> &v) {
  ll sz = SIZE(v);
  vc<pair<T, ll>> ret;
  if (sz == 0)
    return ret;
  ll cnt = 1;
  REP(i, 1, sz) {
    if (v[i] == v[i - 1])
      ++cnt;
    else {
      ret.emplace_back(v[i - 1], cnt);
      cnt = 1;
    }
  }
  ret.emplace_back(v[sz - 1], cnt);
  return ret;
}

// Problem
void solve() {
  ll n;
  cin >> n;
  vll a(n), b(n);
  cin >> a >> b;

  vc<pll> cnts_a = vec_continuous_cnts(a);
  vc<pll> cnts_b = vec_continuous_cnts(b);

  vll acc_a(n + 1, 0), acc_b(n + 1, 0); // 値→その値以下の総数
  {
    for (auto [num, cnt] : cnts_a) {
      acc_a[num] = cnt;
    }
    for (auto [num, cnt] : cnts_b) {
      acc_b[num] = cnt;
    }
    REPM(i, 1, n) if (acc_a[i] + acc_b[i] > n) {
      Yn(false);
      return;
    }
  }
  REPM(i, 1, n) {
    acc_a[i] += acc_a[i - 1];
    acc_b[i] += acc_b[i - 1];
  }
  // dump(acc_a, acc_b);
  ll ma = 0;
  REPM(i, 1, n) if (chmax(ma, acc_a[i] - acc_b[i - 1])) {
    // dump(i, ma); //
  }
  // dump(acc_a, acc_b, ma);
  // if (ma * 2 > n) {
  //     Yn(false);
  //     return;
  // }
  // rotate(b.begin(), b.begin() + ma, b.end());
  Yn(true);
  // dump(b, ma);
  REP(i, 0, n) cout << b[(i - ma + n) % n] << (i == n - 1 ? '\\n' : ' ');
}

// entry point
int main() {
  solve();
  return 0;
}


Error: 0

Fixed submission:

/* #region Head */

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using vll = vc<ll>;
using vvll = vvc<ll>;
using vld = vc<ld>;
using vvld = vvc<ld>;
using vs = vc<string>;
using vvs = vvc<string>;
template <class T, class U> using um = unordered_map<T, U>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqa = priority_queue<T, vc<T>, greater<T>>;
template <class T> using us = unordered_set<T>;

#define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i))
#define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i))
#define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i))
#define REPD(i, m, n, d)                                                       \\
  for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d))
#define REPMD(i, m, n, d)                                                      \\
  for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d))
#define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) begin(x), end(x)
#define SIZE(x) ((ll)(x).size())
#define PERM(c)                                                                \\
  sort(ALL(c));                                                                \\
  for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define UNIQ(v) v.erase(unique(ALL(v)), v.end());

#define endl '\\n'
#define sqrt sqrtl
#define floor floorl
#define log2 log2l

constexpr ll INF = 1'010'000'000'000'000'017LL;
constexpr int IINF = 1'000'000'007LL;
constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.14159265358979323846;

template <typename T>
istream &operator>>(istream &is, vc<T> &vec) { // vector 入力
  for (T &x : vec)
    is >> x;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vc<T> &vec) { // vector 出力 (for dump)
  os << "{";
  REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "" : ", ");
  os << "}";
  return os;
}
template <typename T>
ostream &operator>>(ostream &os, vc<T> &vec) { // vector 出力 (inline)
  REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "\\n" : " ");
  return os;
}

template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &pair_var) { // pair 入力
  is >> pair_var.first >> pair_var.second;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) { // pair 出力
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}

// map, um, set, us 出力
template <class T> ostream &out_iter(ostream &os, T &map_var) {
  os << "{";
  REPI(itr, map_var) {
    os << *itr;
    auto itrcp = itr;
    if (++itrcp != map_var.end())
      os << ", ";
  }
  return os << "}";
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  return out_iter(os, map_var);
}
template <typename T, typename U>
ostream &operator<<(ostream &os, um<T, U> &map_var) {
  os << "{";
  REPI(itr, map_var) {
    auto [key, value] = *itr;
    os << "(" << key << ", " << value << ")";
    auto itrcp = itr;
    if (++itrcp != map_var.end())
      os << ", ";
  }
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
  return out_iter(os, set_var);
}
template <typename T> ostream &operator<<(ostream &os, us<T> &set_var) {
  return out_iter(os, set_var);
}
template <typename T> ostream &operator<<(ostream &os, pq<T> &pq_var) {
  pq<T> pq_cp(pq_var);
  os << "{";
  if (!pq_cp.empty()) {
    os << pq_cp.top(), pq_cp.pop();
    while (!pq_cp.empty())
      os << ", " << pq_cp.top(), pq_cp.pop();
  }
  return os << "}";
}

// dump
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) > 0)
    DUMPOUT << ", ";
  dump_func(move(tail)...);
}

// chmax (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}

// chmin (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

// ローカル用
#define DEBUG_

#ifdef DEBUG_
#define DEB
#define dump(...)                                                              \\
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                              \\
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl  \\
          << "    ",                                                           \\
      dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

struct AtCoderInitialize {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;
  AtCoderInitialize() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} ATCODER_INITIALIZE;

void Yn(bool p) { cout << (p ? "Yes" : "No") << endl; }
void YN(bool p) { cout << (p ? "YES" : "NO") << endl; }

/* #endregion */

template <typename T> vc<pair<T, ll>> vec_continuous_cnts(vc<T> &v) {
  ll sz = SIZE(v);
  vc<pair<T, ll>> ret;
  if (sz == 0)
    return ret;
  ll cnt = 1;
  REP(i, 1, sz) {
    if (v[i] == v[i - 1])
      ++cnt;
    else {
      ret.emplace_back(v[i - 1], cnt);
      cnt = 1;
    }
  }
  ret.emplace_back(v[sz - 1], cnt);
  return ret;
}

// Problem
void solve() {
  ll n;
  cin >> n;
  vll a(n), b(n);
  cin >> a >> b;

  // a の中で，個数が多いものを他に優先的に割り当てていく
  vc<pll> cnts_a = vec_continuous_cnts(a);
  vc<pll> cnts_b = vec_continuous_cnts(b);
  // dump(cnts_a);
  // dump(cnts_b);

  vll cnt_map_b(n + 1, 0);
  for (auto [num, cnt] : cnts_b) {
    cnt_map_b[num] = cnt;
  }
  vll cnt_map_a(n + 1, 0);
  for (auto [num, cnt] : cnts_a) {
    cnt_map_a[num] = cnt;
  }

  vc<pll> cnts_and_nums;
  for (auto [num, cnt] : cnts_a) {
    cnts_and_nums.emplace_back(cnt, num);
  }
  sort(ALL(cnts_and_nums), [&cnt_map_b](const pll &l, const pll &r) -> bool {
    if (cnt_map_b[l.second] != cnt_map_b[r.second]) {
      return cnt_map_b[l.second] > cnt_map_b[r.second]; //
    }
    if (l.first == r.first) {
      return l.second < r.second;
    }
    return l.first > r.first;
  }); // ソート
  // reverse(ALL(cnts_and_nums)); // 降順にする
  // dump(cnts_and_nums);

  // map<ll, ll> num_capacity; // b からわかる，いくつ使えるか
  priority_queue<pll, vc<pll>, function<bool(pll, pll)>> num_que(
      [&cnt_map_a](const pll &a, const pll &b) {
        if (a.first == b.first) {
          if (cnt_map_a[a.second] != cnt_map_a[b.second]) {
            return cnt_map_a[a.second] > cnt_map_a[b.second]; //
          }
          return a.second > b.second;
        }
        return a.first < b.first; //
      }); // 個数に余裕があるものから取り出される
  for (auto [num, cnt] : cnts_b) {
    // num_capacity[num] = cnt; //
    num_que.emplace(cnt, num);
  }

  map<ll, vll> assigned; // 実際に使う数
  for (auto [cnt, num] : cnts_and_nums) {
    // num 以外の数を cnt 個使えばよい
    ll used = 0;
    vll assigned_b;
    bool taihi = false;
    pll taihi_p;
    while (used < cnt && !num_que.empty()) {
      auto [cnt_b, num_b] = num_que.top();
      num_que.pop();
      if (num_b > n)
        num_b -= n;
      if (num_b == num) {
        taihi = true;
        taihi_p = {cnt_b, num_b + n};
        continue;
      }
      while (used < cnt && cnt_b > 0) {
        cnt_b--;
        used++;
        assigned_b.push_back(num_b);
      }
      if (cnt_b > 0) {
        num_que.emplace(cnt_b, num_b);
      }
    }
    // dump(cnt, num, used, assigned_b);

    if (used < cnt) {
      Yn(false);
      return;
    }
    if (taihi) {
      num_que.emplace(taihi_p);
    }
    assigned[num] = assigned_b;
  }
  // dump(assigned);

  vll ret;
  for (auto p_num_cnt : cnts_a) {
    ll num = p_num_cnt.first;
    vll &assigned_b = assigned[num];
    // dump(num, assigned_b);
    copy(ALL(assigned_b), back_inserter(ret));
  }
  Yn(true);
  cout >> ret;
}

// entry point
int main() {
  solve();
  return 0;
}