alexjercan/bugnet huggingface dataset:
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define M 1000005
#define FI first
#define SE second
#define MP std::make_pair
#define PB push_back
#define INF 1000000000
typedef long long ll;
int n, A[M], B[M], X[M], Y[M], cnt[M];
std::mt19937_64
    rnd(std::chrono::steady_clock::now().time_since_epoch().count());
int read() {
  int ans = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    ans = ans * 10 + c - '0', c = getchar();
  return ans;
}
void Write(int x) {
  if (x < 10)
    putchar(x + '0');
  else
    Write(x / 10), putchar(x % 10 + '0');
  return;
}
int Random(int l, int r) { return rnd() % (r - l + 1) + l; }
void work() {
  for (int i = 1; i <= n; i++)
    cnt[i] = 0;
  for (int i = 1; i <= n; i++)
    cnt[A[i]]++, cnt[B[i]]++;
  for (int i = 1; i <= n; i++)
    if (cnt[i] > n)
      return puts("No"), void();
  puts("Yes");
  std::reverse(B + 1, B + 1 + n);
  *X = *Y = 0;
  for (int i = 1; i <= n; i++)
    if (A[i] == B[i])
      X[++*X] = i;
  for (int i = 1; i <= n; i++)
    if (!*X || (A[i] != A[X[1]] && B[i] != A[X[1]]))
      Y[++*Y] = i;
  assert(*X <= *Y);
  while (*X)
    std::swap(B[X[*X]], B[Y[*Y]]), --*X, --*Y;
  for (int i = 1; i <= n; i++)
    assert(A[i] != B[i]);
  for (int i = 1; i <= n; i++)
    Write(B[i]), putchar(' ');
  return putchar('\\n'), void();
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    A[i] = read();
  for (int i = 1; i <= n; i++)
    B[i] = read();
  return work(), 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define M 1000005
#define FI first
#define SE second
#define MP std::make_pair
#define PB push_back
#define INF 1000000000
typedef long long ll;
int n, A[M], B[M], X[M], Y[M], cnt[M];
int read() {
  int ans = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    ans = ans * 10 + c - '0', c = getchar();
  return ans;
}
void Write(int x) {
  if (x < 10)
    putchar(x + '0');
  else
    Write(x / 10), putchar(x % 10 + '0');
  return;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    A[i] = read(), cnt[A[i]]++;
  for (int i = 1; i <= n; i++)
    B[i] = read(), cnt[B[i]]++;
  for (int i = 1; i <= n; i++)
    if (cnt[i] > n)
      return puts("No"), 0;
  puts("Yes");
  std::reverse(B + 1, B + 1 + n);
  for (int i = 1; i <= n; i++)
    if (A[i] == B[i])
      X[++*X] = i;
    else
      Y[++*Y] = i;
  assert(*X <= *Y);
  while (*X)
    std::swap(B[*X], B[*Y]), --*X, --*Y;
  for (int i = 1; i <= n; i++)
    assert(A[i] != B[i]);
  for (int i = 1; i <= n; i++)
    Write(B[i]), putchar(' ');
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define chmax(x, y) x = max(x, y)
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll n, a[200005] = {}, b[200005] = {}, j = 0, bef = -1;

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) {
    if (bef != a[i])
      j = 0;
    if (a[i] == b[i]) {
      while ((a[i] == a[j] || a[i] == b[j]) && j < n)
        j++;
      if (j < n) {
        swap(b[i], b[j]);
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
    bef = a[i];
  }
  cout << "Yes" << endl;
  rep(i, n) cout << b[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define chmax(x, y) x = max(x, y)
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll n, a[200005] = {}, b[200005] = {}, j = 0, ans[200005] = {}, cnt[200005] = {};
bool used[200005] = {};

int main() {
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  rep(i, n) {
    cin >> b[i];
    cnt[b[i]]++;
    if (cnt[b[i]] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    while (a[i] == b[j] || used[j]) {
      j++;
      j %= n;
    }
    ans[i] = b[j];
    used[j] = true;
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++)
    cin >> B[i];
  reverse(B.begin(), B.end());
  for (int i = 0; i < N; i++) {
    if (A[i] == B[i]) {
      int now = N, now2 = N;
      for (int j = i + 1; j < N; j++) {
        if (A[i] != B[j])
          now2 = min(now2, j);
        if (A[i] != B[j] && A[i] != A[j]) {
          now = j;
          break;
        }
      }
      int old = now;
      now = -1;
      for (int j = i - 1; j >= 0; j--)
        if (A[i] != B[j] && A[i] != A[j]) {
          now = j;
          break;
        }
      if (!(now + 1 + N - old < now2 - i)) {
        for (int j = i; j < now2; j++) {
          if (j - i < N - old)
            swap(B[j], B[old + j - i]);
          else
            swap(B[j], B[now - (j - i) + (N - old)]);
        }
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << B[i] << ((i + 1 == N) ? "\\n" : " ");
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++)
    cin >> B[i];
  reverse(B.begin(), B.end());
  for (int i = 0; i < N; i++) {
    if (A[i] == B[i]) {
      int now = N, now2 = N;
      for (int j = i + 1; j < N; j++) {
        if (A[i] != B[j])
          now2 = min(now2, j);
        if (A[i] != B[j] && A[i] != A[j]) {
          now = j;
          break;
        }
      }
      int old = now;
      now = -1;
      for (int j = i - 1; j >= 0; j--)
        if (A[i] != B[j] && A[i] != A[j]) {
          now = j;
          break;
        }
      if (!(now + 1 + N - old < now2 - i)) {
        for (int j = i; j < now2; j++) {
          if (j - i < N - old)
            swap(B[j], B[old + j - i]);
          else
            swap(B[j], B[now - (j - i)]);
        }
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << B[i] << ((i + 1 == N) ? "\\n" : " ");
  }
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int SIZE = 2e5 + 10;
int n;
int a[SIZE], b[SIZE], l[SIZE], r[SIZE];

inline int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch)) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x;
}

int main() {
  int l = SIZE, r = 0, c = 0;
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int j = 1; j <= n; ++j)
    b[j] = read();
  for (int i = 1; i <= (n >> 1); ++i)
    swap(b[i], b[n - i + 1]);
  for (int i = 1; i <= n; ++i)
    if (a[i] == b[i]) {
      r = i;
      if (l > n)
        l = i;
    }
  for (int i = 1; i <= n; ++i)
    if (a[i] != a[r] && b[i] != a[r])
      ++c;
  if (c < r - l + 1)
    printf("No\\n");
  else {
    printf("Yes\\n");
    for (int i = 1; i <= n; ++i) {
      if (l > r)
        break;
      if (a[i] != a[l] && b[i] != a[l])
        swap(b[i], b[l++]);
    }
    for (int i = 1; i <= n; ++i)
      printf("%d ", b[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int SIZE = 2e5 + 10;
int n;
int a[SIZE], b[SIZE], l[SIZE], r[SIZE];

inline int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch)) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x;
}

int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int j = 1; j <= n; ++j)
    b[j] = read();
  for (int i = 1; i <= (n >> 1); ++i)
    swap(b[i], b[n - i + 1]);
  for (int i = 1; i <= n + 1; ++i) {
    if (b[i] != b[i - 1])
      l[b[i]] = i, r[b[i - 1]] = i - 1;
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) {
      int d = 1;
      while (d <= n) {
        if (b[d] != b[i]) {
          swap(b[d], b[i]);
          break;
        } else
          d = r[b[d]] + 1;
      }
    }
  }
  int p = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) {
      p = 1;
      printf("No\\n");
      break;
    }
  }
  if (!p) {
    printf("Yes\\n");
    for (int i = 1; i <= n; ++i)
      printf("%d ", b[i]);
  }
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr(n), brr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> brr[i];
  }
  reverse(brr.begin(), brr.end());
  int number = -1;
  for (int i = 0; i < n; i++) {
    if (arr[i] == brr[i]) {
      number = arr[i];
      break;
    }
  }
  int leftPos = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] == number && brr[i] == number) {
      leftPos = i;
      break;
    }
  }
  int rightPos = -1;
  for (int i = n - 1; i >= 0; i--) {
    if (arr[i] == number && brr[i] == number) {
      rightPos = i;
      break;
    }
  }
  for (int i = 0; i < n && number != -1; i++) {
    if (arr[i] != number && brr[i] != number && leftPos <= rightPos) {
      swap(brr[i], brr[leftPos]);
      leftPos++;
    }
  }
  if (leftPos <= rightPos) {
    cout << "No\\n";
  } else {
    cout << "Yes\\n";
    for (int x : brr)
      cout << x << " ";
    cout << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9 + 7;
const int N = 1e6 + 5;
ll a[N];
ll b[N];
ll c[N];
ll ca[N];
bool vis[N];
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  for (int tc = 1; tc <= 700; tc++) {
    random_shuffle(b + 1, b + 1 + n);
    bool can = true;
    for (int i = 1; i <= n; i++) {
      if (a[i] == b[i])
        can = false;
    }
    if (can) {
      cout << "Yes" << endl;
      for (int i = 1; i <= n; i++)
        cout << b[i] << " ";
      cout << endl;
      return;
    }
  }
  cout << "No" << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Main {
  int N;
  vector<int> A, B;
  void input() {
    cin >> N;
    A.resize(N);
    B.resize(N);
    REP(i, N) { cin >> A[i]; }
    REP(i, N) { cin >> B[i]; }
  }
  void output() {
    int prev = -1;
    int j = 0;
    REP(i, N) {
      if (A[i] != prev)
        j = 0;
      if (A[i] == B[i]) {
        for (; j < N; ++j) {
          if (A[i] != B[j] && A[j] != B[i]) {
            swap(B[i], B[j]);
            break;
          }
        }
        if (A[i] == B[i]) {
          cout << "No" << endl;
          return;
        }
      }
      prev = A[i];
    }
    cout << "Yes" << endl;
    REP(i, N) { cout << B[i] << " "; }
    cout << endl;
  }

public:
  Main() {
    input();
    output();
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  Main();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Main {
  int N;
  vector<int> A, B;
  void input() {
    cin >> N;
    A.resize(N);
    B.resize(N);
    REP(i, N) { cin >> A[i]; }
    REP(i, N) { cin >> B[i]; }
  }
  void output() {
    int l = 0;
    int front = 0;
    while (true) {
      while (l < N && A[l] != B[l])
        ++l;
      if (l == N)
        break;
      int r = UB(B, B[l]);
      while (r < N && A[r] != B[r])
        ++r;
      if (r == N) {
        if (front == N) {
          cout << "No" << endl;
          return;
        }
        swap(B[front], B[l]);
        ++front;
      } else {
        swap(B[l], B[r]);
      }
    }
    REP(i, N) {
      if (A[i] == B[i]) {
        cout << "No" << endl;
        return;
      }
    }
    cout << "Yes" << endl;
    REP(i, N) { cout << B[i] << " "; }
    cout << endl;
  }

public:
  Main() {
    input();
    output();
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  Main();

  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll A[200009], B[200009];
vector<pair<ll, ll>> vec;
int main() {
  ll a, b, c, d, e, i, j, k, l, n, m, x, y, t, p;
  scanf("%lld", &n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &A[i]);
  }
  for (i = 0; i < n; i++) {
    scanf("%lld", &B[i]);
  }
  sort(B, B + n, greater<ll>());
  for (i = 0; i < n; i++) {
    if (A[i] == B[i]) {
      vec.push_back({A[i], i});
    }
  }
  l = vec.size();
  j = 0;
  for (i = 0; i < n; i++) {
    if (j == l) {
      break;
    }
    if (A[i] != vec[j].first && B[i] != vec[j].first) {
      swap(B[i], B[vec[j].second]);
      j++;
    }
  }
  if (j == l) {
    printf("Yes\\n");
    for (i = 0; i < n; i++) {
      printf("%lld ", B[i]);
    }
    printf("\\n");
  } else {
    printf("No\\n");
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll A[200009], B[200009], ans[200009];
ll cnta[200009], cntb[200009];
vector<pair<ll, ll>> vec;
vector<ll> temp, temp2, temp3;
int main() {
  ll a, b, c, d, e, i, j, k, l, n, m, x, y, t, p;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a);
    A[i] = a;
    vec.push_back({a, i});
    cnta[a]++;
  }
  for (i = 1; i <= n; i++) {
    scanf("%lld", &b);
    B[i] = b;
    cntb[b]++;
  }
  for (i = 1; i <= n; i++) {
    if (cnta[i] > 0) {
      for (j = 0; j < cntb[i]; j++) {
        temp.push_back(i);
      }
    } else {
      for (j = 0; j < cntb[i]; j++) {
        temp2.push_back(i);
      }
    }
  }
  sort(vec.begin(), vec.end());
  memset(ans, -1, sizeof(ans));
  sort(temp.rbegin(), temp.rend());
  l = temp.size();
  i = j = 0;
  while (1) {
    if (i == n || j == l) {
      break;
    }

    if (vec[i].first != temp[j]) {
      ans[vec[i].second] = temp[j];
      i++;
      j++;
    } else {
      temp3.push_back(temp[j]);
      j++;
    }
  }
  for (; j < l; j++) {
    temp3.push_back(temp[j]);
  }
  l = temp3.size();
  for (i = l - 1; i >= 0; i--) {
    temp2.push_back(temp3[i]);
  }
  j = 0;
  for (; i < n; i++) {
    ans[vec[i].second] = temp2[j];
    j++;
  }
  c = 1;

  for (i = 1; i <= n; i++) {
    if (ans[i] == A[i]) {
      c = 0;
      break;
    }
  }

  if (c) {
    printf("Yes\\n");
    for (i = 1; i <= n; i++) {
      printf("%lld ", ans[i]);
    }
    printf("\\n");

  } else {
    printf("No\\n");
  }

  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  sort(b.rbegin(), b.rend());
  int left = 0;
  int right = n - 1;
  rep(i, n) {
    if (a[i] == b[i]) {
      if ((b[left] == b[i] || a[left] == b[i]) &&
          (b[right] == b[i] || a[right] == b[i])) {
        cout << "No" << endl;
        return 0;
      }
      if (b[left] != b[i] && a[left] != b[i]) {
        swap(b[left], b[i]);
        left++;
      } else if (b[right] != b[i] && a[right] != b[i]) {
        swap(b[right], b[i]);
        right--;
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n) cout << b[i] << " ";
  cout << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  int right = n - 1;
  rep(i, n) {
    if (i == right)
      break;
    if (a[i] == b[i] || a[right] == b[right]) {
      if (b[i] == b[right]) {
        cout << "No" << endl;
        return 0;
      }
      swap(b[i], b[right]);
      right--;
    }
  }
  if (a[right] == b[right]) {
    bool ok = false;
    rep(i, n) {
      if (b[i] != b[right] && a[i] != b[right]) {
        swap(b[i], b[right]);
        ok = true;
        break;
      }
    }
    if (ok) {
      cout << "Yes" << endl;
      rep(i, n) cout << b[i] << " ";
      cout << endl;
    } else
      cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  rep(i, n) cout << b[i] << " ";
  cout << endl;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, a[400001], b[400001], ta[400001], tb[400001], l = 1, r = 1;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), ta[a[i]]++;
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]), tb[b[i]]++;
  for (int i = 1; i <= n; i++)
    if (ta[i] + tb[i] > n)
      puts("No"), exit(0);
  memset(b, 0, sizeof(b));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= tb[i]; j++) {
      while (b[l])
        ++l;
      while (a[r] <= a[l] && r <= n)
        ++r;
      while (b[r])
        ++r;
      if (a[l] != i)
        b[l++] = i;
      else
        b[r++] = i;
    }
  }
  l = 1;
  for (int i = n + 1; b[i]; i++) {
    while (b[l])
      ++l;
    if (a[l] != b[i])
      a[l] = b[i];
    else {
      while (a[r = (rand() % n + 1)] == b[i] || b[r] == a[l])
        ;
      b[l] = b[r], b[r] = b[i];
    }
  }
  puts("Yes");
  for (int i = 1; i <= n; i++)
    printf("%d ", b[i]);
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
// #define int long long
#define rep(i, l, r) for (int i = l; i <= r; ++i)
using namespace std;
const int MAXN = 1e5 + 10, mod = 1e9 + 7, INF = 1e9;

struct node {
  int val, pcs;
} q[MAXN];

int tail;
int a[MAXN], b[MAXN], buca[MAXN], bucb[MAXN];

signed main() {
  int n;
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &a[i]), buca[a[i]]++;
  rep(i, 1, n) scanf("%d", &b[i]), bucb[b[i]]++;
  bool ok(1);
  rep(i, 1, n) {
    if (n - bucb[a[i]] < buca[a[i]])
      ok = 0;
    if (bucb[i])
      q[++tail] = {i, bucb[i]};
  }
  if (!ok) {
    puts("No");
    return 0;
  }
  puts("Yes");
  rep(i, 1, n) {
    int cur = tail;
    while (q[cur].val == a[i] || q[cur].pcs == 0)
      --cur;
    q[cur] = {q[cur].val, q[cur].pcs - 1};
    printf("%d ", q[cur].val);
  }
  return 0;
}