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

#include <bits/stdc++.h>
typedef long long ll;
typedef std::pair<ll, ll> P;

#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;

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

const ll mod = 1000000007;

ll mpow(ll x, ll n) {
  ll ans = 1;
  x %= mod;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

ll inv_mod(ll a) { return mpow(a, mod - 2); }

class Factorial {
private:
  vector<ll> fac;
  vector<ll> ifac;

public:
  Factorial(ll N) {
    fac.push_back(1);
    for (int i = 0; i < N; i++)
      fac.push_back(fac[i] * (i + 1) % mod);
    ifac.resize(N + 1);
    ifac[N] = inv_mod(fac[N]);
    for (int i = 0; i < N; i++)
      ifac[N - 1 - i] = (ifac[N - i] * (N - i)) % mod;
  }

  ll fact(ll a) { return fac[a]; }
  ll ifact(ll a) { return ifac[a]; }

  ll cmb(ll a, ll b) {
    if (a == 0 && b == 0)
      return 1;
    if (a < b || a < 0 || b < 0)
      return 0;
    ll tmp = ifact(a - b) * ifact(b) % mod;
    return tmp * fac[a] % mod;
  }
  ll per(ll a, ll b) {
    if (a == 0 && b == 0)
      return 1;
    if (a < b || a < 0 || b < 0)
      return 0;
    return fac[a] * ifac[a - b] % mod;
  }
};

class modint {
  static constexpr std::int_fast64_t Modulus = 1e9 + 7;
  using u64 = std::int_fast64_t;

public:
  u64 a;
  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr operator u64() const { return a; }
};

int main() {
  ll n;
  cin >> n;
  vector<ll> as(n), bs(n);
  rep(i, n) cin >> as[i];
  rep(i, n) cin >> bs[i];
  int j = 0;
  for (int i = 0; i < n; i++) {
    int error = 0;
    bool ok = false;
    for (; j < n; j++) {
      if (as[j] != bs[i] && as[i] != bs[j]) {
        swap(bs[i], bs[j]);
        ok = true;
        break;
      }
      error++;
      if (error == n) {
        cout << "No" << endl;
        return 0;
      }
    }
    if (ok)
      continue;
    j = 0;
    for (; j < n; j++) {
      if (as[j] != bs[i] && as[i] != bs[j]) {
        swap(bs[i], bs[j]);
        break;
      }
      error++;
      if (error == n) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n) { cout << bs[i] << " "; }
  cout << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef std::pair<ll, ll> P;

#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;

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

const ll mod = 1000000007;

ll mpow(ll x, ll n) {
  ll ans = 1;
  x %= mod;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

ll inv_mod(ll a) { return mpow(a, mod - 2); }

class Factorial {
private:
  vector<ll> fac;
  vector<ll> ifac;

public:
  Factorial(ll N) {
    fac.push_back(1);
    for (int i = 0; i < N; i++)
      fac.push_back(fac[i] * (i + 1) % mod);
    ifac.resize(N + 1);
    ifac[N] = inv_mod(fac[N]);
    for (int i = 0; i < N; i++)
      ifac[N - 1 - i] = (ifac[N - i] * (N - i)) % mod;
  }

  ll fact(ll a) { return fac[a]; }
  ll ifact(ll a) { return ifac[a]; }

  ll cmb(ll a, ll b) {
    if (a == 0 && b == 0)
      return 1;
    if (a < b || a < 0 || b < 0)
      return 0;
    ll tmp = ifact(a - b) * ifact(b) % mod;
    return tmp * fac[a] % mod;
  }
  ll per(ll a, ll b) {
    if (a == 0 && b == 0)
      return 1;
    if (a < b || a < 0 || b < 0)
      return 0;
    return fac[a] * ifac[a - b] % mod;
  }
};

class modint {
  static constexpr std::int_fast64_t Modulus = 1e9 + 7;
  using u64 = std::int_fast64_t;

public:
  u64 a;
  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr operator u64() const { return a; }
};

int main() {
  ll n;
  cin >> n;
  vector<ll> as(n), bs(n);
  rep(i, n) cin >> as[i];
  rep(i, n) cin >> bs[i];
  reverse(bs.begin(), bs.end());

  ll start = -1;
  rep(i, n) {
    if (as[i] == bs[i]) {
      start = i;
      break;
    }
  }
  if (start == -1) {
    cout << "Yes" << endl;
    rep(i, n) { cout << bs[i] << " "; }
    cout << endl;
    return 0;
  }
  ll finish = start;
  for (int i = start; i < n; i++) {
    if (as[i] != bs[i]) {
      break;
    }
    finish = i;
  }
  int left = 0;
  int right = n - 1;
  bool isLeft = true;
  for (int i = start; i <= finish; i++) {
    if (isLeft) {
      while (bs[left] == as[i]) {
        left++;
        if (left == start) {
          isLeft = false;
          break;
        }
      }
      if (isLeft) {
        swap(bs[left], bs[i]);
      }
    }
    if (!isLeft) {
      while (bs[right] == as[i]) {
        right--;
        if (right == finish) {
          cout << "No" << endl;
          return 0;
        }
      }
      swap(bs[right], bs[i]);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) { cout << bs[i] << " "; }
  cout << endl;
  return 0;
}