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

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vector<int> a(N), b(N);
  cin >> a >> b;

  vector<int> ac(N + 1), bc(N + 1);
  REP(i, N) {
    ac[a[i]]++;
    bc[b[i]]++;
  }
  REP(i, N) {
    ac[i + 1] += ac[i];
    bc[i + 1] += bc[i];
  }
  int rot = 0;
  REP(i, N) {
    CHMAX(rot, ac[i + 1] - bc[i]);
    if (ac[i + 1] - ac[i] + bc[i + 1] - bc[i] > N) {
      out("No");
      return 0;
    }
  }
  out("Yes");
  REP(i, N) { cout << b[(i - rot + N) % N] << " \\n"[i + 1 == N]; }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int INF = 1e18;

#define endl '\\n'
#define IOS()                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)

signed main() {
  IOS();
  int N;
  cin >> N;
  vector<int> a(N), b(N);
  cin >> a >> b;
  vector<int> cntA(N), cntB(N);
  REP(i, N) {
    --a[i];
    --b[i];
    ++cntA[a[i]];
    ++cntB[b[i]];
  }
  vector<int> idx(N);
  iota(ALL(idx), 0);
  sort(ALL(idx), [&](int l, int r) {
    if (cntA[l] > cntA[r])
      return true;
    else if (cntB[l] > cntB[r])
      return true;
    else
      return l < r;
  });
  vector<int> inv(N);
  REP(i, N) inv[idx[i]] = i;
  sort(ALL(a), [&](int l, int r) { return inv[l] < inv[r]; });
  sort(ALL(b), [&](int l, int r) { return inv[l] < inv[r]; });
  int cnt = 0;
  while (cnt < N && a[0] == b[cnt])
    ++cnt;
  vector<int> v(N);
  FOR(i, cnt, N) v[i - cnt] = b[i];
  REP(i, cnt) v[i + cnt] = b[i];
  REP(i, N) {
    if (a[i] == v[i]) {
      // REP(i,a.size())cout << a[i] << " \\n"[i+1 == a.size()];
      // REP(i,v.size())cout << v[i] << " \\n"[i+1 == v.size()];
      out("No");
      return 0;
    }
  }
  vector<pair<int, int>> aa(N);
  REP(i, N) aa[i] = {a[i], i};
  sort(ALL(aa));
  // REP(i,aa.size())cout << aa[i].first << " \\n"[i+1 == aa.size()];
  vector<int> ans(N);
  REP(i, N) ans[i] = v[aa[i].second];
  out("Yes");
  // REP(i,a.size())cout << a[i] << " \\n"[i+1 == a.size()];
  REP(i, v.size()) cout << ans[i] + 1 << " \\n"[i + 1 == v.size()];
}