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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr << (a) << endl
#define dbrep(a, n)                                                            \\
  rep(_i, n) cerr << (a[_i]) << " ";                                           \\
  cerr << endl
#define dbrep2(a, n, m)                                                        \\
  rep(_i, n) {                                                                 \\
    rep(_j, m) cerr << (a[_i][_j]) << " ";                                     \\
    cerr << endl;                                                              \\
  }

using namespace std;

template <class A, class B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << "(" << p.fi << "," << p.se << ")";
}
template <class A, class B> istream &operator>>(istream &is, pair<A, B> &p) {
  return is >> p.fi >> p.se;
}

/* Some Libraries */

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vi a(N), A(N + 1), B(N + 1), D(N + 1);
  rep(i, N) {
    cin >> a[i];
    A[a[i]]++;
    D[a[i]]++;
  }
  rep(i, N) {
    int b;
    cin >> b;
    B[b]++;
    D[b]++;
  }

  if (*max_element(all(D)) > N) {
    cout << "No\\n";
    return 0;
  }
  cout << "Yes\\n";

  auto solve = [&](vector<pi> &vp) {
    int V = vp.size();
    vi next(N + 1, -1);
    rep(i, V) { next[vp[i].se] = vp[(i + 1) % V].se; }

    vector<queue<int>> ans(N + 1);
    int cur = vp[0].se;
    rep(i, V) {
      int t = vp[i].se;
      if (cur == t) {
        cur = next[cur];
      }
      int cnt = B[t];
      while (cnt > 0) {
        if (cur == t)
          return;
        while (A[cur] > 0 && cnt > 0) {
          A[cur]--;
          cnt--;
          ans[cur].push(t);
        }
        if (cnt == 0)
          break;
        cur = next[cur];
      }
    }
    rep(i, N) {
      if (i > 0)
        cout << " ";
      cout << ans[a[i]].front();
      ans[a[i]].pop();
    }
    cout << "\\n";
    exit(0);
    return;
  };

  vector<pi> vec;
  rep(i, N + 1) if (D[i] > 0) vec.pb({D[i], i});
  sort(rall(vec));
  solve(vec);

  vec = vector<pi>();
  rep(i, N + 1) if (D[i] > 0) vec.pb({A[i], i});
  sort(rall(vec));
  solve(vec);

  vec = vector<pi>();
  rep(i, N + 1) if (D[i] > 0) vec.pb({B[i], i});
  sort(rall(vec));
  solve(vec);

  vec = vector<pi>();
  rep(i, N + 1) if (D[i] > 0) vec.pb({A[i] - B[i], i});
  sort(rall(vec));
  solve(vec);

  vec = vector<pi>();
  rep(i, N + 1) if (D[i] > 0) vec.pb({B[i] - A[i], i});
  sort(rall(vec));
  solve(vec);

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr << (a) << endl
#define dbrep(a, n)                                                            \\
  rep(_i, n) cerr << (a[_i]) << " ";                                           \\
  cerr << endl
#define dbrep2(a, n, m)                                                        \\
  rep(_i, n) {                                                                 \\
    rep(_j, m) cerr << (a[_i][_j]) << " ";                                     \\
    cerr << endl;                                                              \\
  }

using namespace std;

template <class A, class B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << "(" << p.fi << "," << p.se << ")";
}
template <class A, class B> istream &operator>>(istream &is, pair<A, B> &p) {
  return is >> p.fi >> p.se;
}

/* Some Libraries */

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vi a(N), b(N);
  rep(i, N) cin >> a[i];
  rep(i, N) cin >> b[i];

  vi A(N + 1), B(N + 1);
  rep(i, N) A[a[i]]++;
  rep(i, N) B[b[i]]++;
  vi p(N + 1);
  rep(i, N) p[a[i]]++, p[b[i]]++;

  vector<pi> aa;
  rep(i, N + 1) { aa.pb({p[i], i}); }
  sort(rall(aa));

  vi vec;
  for (auto e : aa) {
    rep(i, e.fi) vec.pb(e.se);
  }

  FOR(i, 0, N) {
    if (p[i] > N) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  vector<pi> vec2;
  rep(i, N) { vec2.pb({vec[i], vec[i + N]}); }
  vector<queue<int>> ans(N + 1);
  rep(i, N) {
    auto t = vec2[i];
    if (A[t.fi] > 0 && B[t.se] > 0) {
      A[t.fi]--;
      B[t.se]--;
      ans[t.fi].push(t.se);
    } else if (A[t.se] > 0 && B[t.fi] > 0) {
      A[t.se]--;
      B[t.fi]--;
      ans[t.se].push(t.fi);
    }
  }
  rep(i, N) {
    int tt = ans[a[i]].front();
    ans[a[i]].pop();
    cout << tt << " ";
  }
  cout << endl;
  return 0;
}