alexjercan/bugnet huggingface dataset:
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

// #include <atcoder/all>

using namespace std;
// using namespace atcoder;

// #define int long long
#define ll long long

ll INF = (1LL << 60);
int mod = 1000000007;

using P = pair<int, int>;

signed 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];
  map<int, int> cnt;
  for (int i = 0; i < N; i++) {
    cnt[A[i]]++;
    cnt[B[i]]++;
  }
  for (int i = 1; i <= 200000; i++) {
    if (cnt[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  reverse(B.begin(), B.end());
  for (int i = 0; i < N; i++) {
    if (A[i] == B[i]) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          continue;
        if (A[i] != B[j] && A[j] != B[i]) {
          swap(B[i], B[j]);
          break;
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << B[i];
    if (i == N - 1)
      cout << endl;
    else
      cout << " ";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
// #include <atcoder/all>

using namespace std;
// using namespace atcoder;

#define int long long
#define ll long long

ll INF = (1LL << 60);
int mod = 1000000007;

signed main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
    B[i]--;
  }
  reverse(B.begin(), B.end());
  int cnt = 0;

  for (int i = 0; i < N; i++) {
    if (A[i] != B[i])
      cnt++;
  }
  int now = 0;
  while (now <= 3000000) {
    now++;
    std::random_device rnd; // 非決定的な乱数生成器を生成
    std::mt19937 mt(
        rnd()); //  メルセンヌ・ツイスタの32ビット版、引数は初期シード値
    std::uniform_int_distribution<> randa(0, N - 1), randb(0, N - 1);
    int i = randa(mt), j = randb(mt);
    if (i == j)
      continue;
    if (A[i] == B[j])
      continue;
    if (A[j] == B[i])
      continue;
    if (A[i] == B[i])
      cnt--;
    if (A[j] == B[j])
      cnt--;
    swap(B[i], B[j]);
    if (cnt == 0) {
      cout << "Yes" << endl;
      for (int i = 0; i < N; i++) {
        cout << B[i] + 1 << " ";
      }
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, a[202020], b[202020];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
  }
  reverse(b, b + n);
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      continue;
    }
    bool ok = false;
    for (int j = 0; j < n; j++) {
      if (i == j) {
        continue;
      }
      if (a[i] != b[j] && a[j] != b[i]) {
        swap(b[i], b[j]);
        ok = true;
        break;
      }
    }
    if (!ok) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    printf("%d ", b[i]);
  }
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, a[202020], b[202020];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      continue;
    }
    bool ok = false;
    for (int j = 0; j < n; j++) {
      if (i == j) {
        continue;
      }
      if (a[i] != b[j] && a[j] != b[i]) {
        swap(b[i], b[j]);
        ok = true;
        break;
      }
    }
    if (!ok) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    cout << b[i] << ' ';
  }
  cout << endl;
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[n], b[n];
  map<int, int> aa;
  rep(i, n) {
    cin >> a[i];
    if (aa[a[i]] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  rep(i, n) {
    cin >> b[i];
    if (aa[b[i]] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  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[i] != a[j]) {
          swap(b[i], b[j]);
          break;
        }
      }
      if (a[i] == b[i]) {
        cout << "No" << endl;
        return 0;
      }
    }
    prev = a[i];
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i)
      cout << " ";
    cout << b[i];
  }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[n], b[n];
  vector<int> ans;
  deque<int> x;
  map<int, int> aa;
  rep(i, n) {
    cin >> a[i];
    aa[a[i]]++;
  }
  rep(i, n) {
    cin >> b[i];
    aa[b[i]]++;
    x.push_back(b[i]);
    if (aa[b[i]] > n) {
      cout << "No" << endl;
      return 0;
    }
  }

  int now = 0;
  while (x.size() != 0) {
    if (x.front() != a[now]) {
      now++;
      ans.push_back(x.front());
      x.pop_front();
    } else {
      x.push_back(x.front());
      x.pop_front();
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i)
      cout << " ";
    cout << ans[i];
  }
  cout << endl;

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

#include "bits/stdc++.h"
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using ll = long long int;

const int mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

  int n;
  cin >> n;
  map<int, int> m;
  vector<int> a(n), b(n);
  for (auto &i : a) {
    cin >> i;
    ++m[i];
  }
  for (auto &i : b) {
    cin >> i;
    ++m[i];
  }
  for (auto [x, y] : m)
    if (y > n)
      return cout << "No\\n", 0;
  reverse(begin(b), end(b));
  int l = 0, r = -1, intersect = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i])
      continue;
    intersect = a[i];
    if (r == -1)
      l = i;
    r = i;
  }
  set<int> active;
  for (int i = 0; i < n; ++i) {
    if (a[i] == intersect || b[i] == intersect)
      continue;
    active.insert(i);
  }
  for (int i = l; i <= r; ++i) {
    auto u = *active.begin();
    swap(b[i], b[u]);
    active.erase(u);
  }
  cout << "Yes\\n";
  for (auto x : b)
    cout << x << ' ';
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using ll = long long int;

const int mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

  int n;
  cin >> n;
  map<int, int> m;
  vector<int> a(n), b(n);
  for (auto &i : a) {
    cin >> i;
    ++m[i];
  }
  for (auto &i : b) {
    cin >> i;
    ++m[i];
  }
  reverse(begin(b), end(b));
  set<int> bad;
  set<int> vals;
  int intersect = -1;
  int l = 0, r = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i])
      continue;
    bad.insert(i);
    l = l == -1 ? i : l;
    r = i;
    intersect = a[i];
  }
  assert(r - l + 1 <= (int)bad.size());
  set<int> active;
  for (int i = 0; i < n; ++i) {
    if (a[i] == intersect || b[i] == intersect)
      continue;
    active.insert(i);
  }
  for (auto i : bad) {
    if (active.empty())
      return cout << "No\\n", 0;
    auto u = *active.begin();
    swap(b[i], b[u]);
    active.erase(u);
  }
  cout << "Yes\\n";
  for (auto x : b)
    cout << x << ' ';
  cout << '\\n';
}

// Suppose a[i] = b[i] and a[j] = b[j], i < j
// i < j -> a[i] <= a[j], b[i] >= b[j]
// a[i] + x = b[i] - y for some x, y >= 0
// x + y = 0
// x = y = 0

// Suppose intersection is with x
// c1x in a, c2x in b, c3x common
// c1x + c2x <= n
// n - c1x - c2x + c3x free spots
// Claim: c3x <= n - c1x - c2x + c3x
// 0 <= n-c1x-c2x
// True
// Why WA?????
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<int> numa(n + 1, 0), numb(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    numa[a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    numb[b[i]]++;
  }
  reverse(b.begin(), b.end());

  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      int j;
      if (numa[a[i]] + numb[b[i]] > n) {
        cout << "No" << endl;
        return 0;
      }
      if (i % 2 == 0) {
        for (j = 0; j < n; j++) {
          if (i == j)
            continue;
          if (a[i] != a[j] && a[i] != b[j]) {
            swap(b[i], b[j]);
            break;
          }
        }
      } else {
        for (j = n - 1; j >= 0; j--) {
          if (i == j)
            continue;
          if (a[i] != a[j] && a[i] != b[j]) {
            swap(b[i], b[j]);
            break;
          }
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n - 1; i++) {
    cout << b[i] << " ";
  }
  cout << b[n - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<int> numa(n + 1, 0), numb(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    numa[a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    numb[b[i]]++;
  }

  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      int j;
      if (numa[a[i]] + numb[b[i]] > n) {
        cout << "No" << endl;
        return 0;
      }
      for (j = 0; j < n; j++) {
        if (i == j)
          continue;
        if (a[i] != a[j] && a[i] != b[j]) {
          swap(b[i], b[j]);
          break;
        }
      }
      if (j == n) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n - 1; i++) {
    cout << b[i] << " ";
  }
  cout << b[n - 1] << endl;

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

// Author : heyuhhh
// Created Time : 2020/09/13 21:37:08
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
// head
const int N = 1e5 + 5;
void run() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<int> cntA(n), cntB(n);
  multiset<int> S;
  vector<vector<int>> G(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    --a[i];
    ++cntA[a[i]];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    --b[i];
    ++cntB[b[i]];
  }
  for (int i = 0; i < n; i++) {
    if (cntA[i] + cntB[i] > n) {
      cout << "No" << '\\n';
      return;
    }
  }
  reverse(all(b));
  int l = n, r = -1, c = -1;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      l = i;
      c = a[i];
      break;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] == b[i]) {
      r = i;
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != c && b[i] != c && l <= r) {
      swap(b[i], b[l]);
      ++l;
    }
  }
  cout << "Yes" << '\\n';
  for (int i = 0; i < n; i++) {
    cout << b[i] + 1 << " \\n"[i == n - 1];
  }
}
int main() {
#ifdef Local
  freopen("input.in", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  run();
  return 0;
}

Error: 0

Fixed submission:

// Author : heyuhhh
// Created Time : 2020/09/13 21:37:08
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
// head
const int N = 1e5 + 5;
void run() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<int> cntA(n + 1), cntB(n + 1);
  multiset<int> S;
  vector<vector<int>> G(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ++cntA[a[i]];
    G[a[i]].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    ++cntB[b[i]];
    S.insert(b[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (cntA[i] + cntB[i] > n) {
      cout << "No" << '\\n';
      return;
    }
  }
  vector<int> res(n);
  for (int i = n; i >= 1; i--) {
    for (auto &it : G[i]) {
      auto p = S.begin();
      if ((*p) == i) {
        p = S.upper_bound(i);
      }
      res[it] = (*p);
      S.erase(p);
    }
  }
  cout << "Yes" << '\\n';
  for (int i = 0; i < n; i++)
    cout << res[i] << " \\n"[i == n - 1];
}
int main() {
#ifdef Local
  freopen("input.in", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  run();
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N], b[N];
int main() {
  int n, si = 0, l = N, r, w = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  for (int i = n; i; i--) {
    scanf("%d", b + i);
    if (b[i] == a[i])
      si++, l = i;
  }
  r = l + si - 1;
  for (int i = 1; i <= n; i++)
    if (b[i] != a[r] && a[i] != a[r])
      w++;
  if (w < si)
    return puts("No"), 0;
  puts("Yes");
  for (int i = 1; i <= n; i++) {
    if (l > r)
      break;
    if (b[i] != a[r] && a[i] != a[r])
      swap(b[i], b[l++]);
  }
  for (int i = 1; i <= n; i++)
    printf("%d ", b[i]);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N], b[N];
int main() {
  int n, si = 0, l, r = 0, w = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  for (int i = n; i; i--) {
    scanf("%d", b + i);
    if (b[i] == a[i])
      si++, r = i;
  }
  l = r - si + 1;
  for (int i = 1; i <= n; i++)
    if (b[i] != a[r] && a[i] != a[r])
      w++;
  if (w < si)
    return puts("No"), 0;
  puts("Yes");
  for (int i = 1; i <= n; i++) {
    if (l > r)
      break;
    if (b[i] != a[r] && a[i] != a[r])
      swap(b[i], b[l++]);
  }
  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() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  map<int, int> mp;
  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());
  int el = -1;
  for (int i = 0; i < n; i++) {
    if (A[i] == B[i]) {
      el = A[i];
      break;
    }
  }
  if (el == -1) {
    cout << "Yes\\n";
    for (int i = 0; i < n; i++)
      cout << B[i] << " ";
    return 0;
  }
  int l, r;
  for (int i = 0; i < n; i++) {
    if (A[i] == el && B[i] == el) {
      l = i;
      break;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (A[i] == el && B[i] == el) {
      r = i;
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    if (l > r)
      break;
    if (A[i] != el && B[i] != el) {
      swap(B[i], B[l]);
      l++;
    }
  }
  if (l <= r) {
    cout << "No\\n";
    return 0;
  }
  cout << "Yes\\n";
  for (int i = 0; i < n; i++)
    cout << B[i] << " ";
  return 0;
}

Error: TLE

Fixed submission:

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

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

  map<int, int> mp;
  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];
  for (int i = 0; i < n; i++) {
    if (A[i] == B[i])
      mp[B[i]]++;
  }
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    if (itr->second > (n / 2)) {
      cout << "No\\n";
      return 0;
    }
  }
  reverse(B.begin(), B.end());
  set<int> st1, st2;
  for (int i = 0; i < n; i++) {
    if (A[i] != B[i])
      st1.insert(i);
    else
      st2.insert(i);
  }
  while (!st2.empty()) {
    int in = *(st2.begin());
    bool f = false;
    for (auto ir = st1.begin(); ir != st1.end(); ir++) {
      int in1 = *ir;
      if (A[in1] != B[in] && B[in1] != B[in]) {
        swap(B[in], B[in1]);
        st1.insert(in);
        f = true;
        break;
      }
    }
    if (f)
      st2.erase(st2.begin());
    else
      break;
  }
  if (!st2.empty()) {
    cout << "No\\n";
    return 0;
  }
  cout << "Yes\\n";
  for (int i = 0; i < n; i++)
    cout << B[i] << " ";
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define deb(x) cout << #x << " " << x << endl;

typedef long long int ll;

int main() {

  IOS

      int n;
  cin >> n;

  int a[n], b[n];

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = n - 1; i >= 0; i--)
    cin >> b[i];

  bool swapped = true;

  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      swapped = false;

      for (int j = 0; j < n; j++) {
        if (b[i] == b[j])
          continue;

        if (a[j] == b[i])
          continue;

        if (a[i] == b[j])
          continue;

        swap(b[i], b[j]);
        swapped = true;
        break;
      }

      if (!swapped)
        break;
    }
  }

  if (swapped) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++)
      cout << b[i] << " ";

  } else
    cout << "No";

  return 0;
}

Error: TLE

Fixed submission:

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define deb(x) cout << #x << " " << x << endl;

typedef long long int ll;

int main() {

  IOS

      int n;
  cin >> n;

  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];

  bool swapped = true;

  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      swapped = false;

      for (int j = 0; j < n; j++) {
        if (b[i] == b[j])
          continue;

        if (a[j] == b[i])
          continue;

        if (a[i] == b[j])
          continue;

        swap(b[i], b[j]);
        swapped = true;
        break;
      }

      if (!swapped)
        break;
    }
  }

  if (swapped) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++)
      cout << b[i] << " ";

  } else
    cout << "No";

  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> x(n), y(n);
  vector<int> ll(n + 1, -1), rr(n + 1, -1), uu(n + 1, -1), vv(n + 1, -1);
  for (auto &xx : x)
    cin >> xx;
  for (auto &xx : y)
    cin >> xx;
  int res = 0;
  vector<int> hh(n + 1, 0);
  for (int k = 1, i = 0, j = 0; k <= n; k++) {
    while (i < n and x[i] < k)
      ++i;
    if (i < n and x[i] == k) {
      int jj = i;
      while (jj < n and x[jj] == k)
        ++jj;
      tie(ll[k], rr[k]) = {i, jj};
      i = jj;
    }
    while (j < n and y[j] < k)
      ++j;
    if (j < n and y[j] == k) {
      int jj = j;
      while (jj < n and y[jj] == k)
        ++jj;
      tie(uu[k], vv[k]) = {j, jj};
      j = jj;
    }
    if (~ll[k] and ~uu[k]) {
      ++res;
      if (rr[k] - ll[k] + vv[k] - uu[k] > n) {
        cout << "No\\n";
        return 0;
      }
      if (uu[k] <= ll[k]) {
        if (vv[k] <= ll[k]) {
          ++hh[0];
          --hh[ll[k] - vv[k] + 1];
          ++hh[rr[k] - uu[k]];
          --hh[n];
        } else {
          ++hh[rr[k] - uu[k]];
          --hh[ll[k] + n - vv[k] + 1];
        }
      } else {
        ++hh[max(0, rr[k] - uu[k])];
        --hh[ll[k] + n - vv[k] + 1];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (hh[i] == res) {
      cout << "Yes\\n";
      rotate(y.begin(), y.begin() + n - i, y.end());
      for (auto xx : y)
        cout << xx << " ";
      cout << endl;
      return 0;
    }
    hh[i + 1] += hh[i];
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n), u1(n), u2(n + 1, -1), v1(n + 1, -1), v2(n + 1, -1);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  for (int i = 0; i < n; i++)
    cin >> y[i];
  int res = 0;
  vector<int> hh(n + 1, 0);
  for (int i = 0, j = 0, k = 1; k <= n; k++) {
    while (i < n and x[i] < k)
      ++i;
    while (i < n and x[i] == k) {
      int jj = i;
      while (jj < n and x[jj] == k)
        ++jj;
      tie(u1[k], u2[k]) = {i, jj};
      i = jj;
    }
    while (j < n and y[j] < k)
      ++j;
    while (j < n and y[j] == k) {
      int jj = j;
      while (jj < n and y[jj] == k)
        ++jj;
      tie(v1[k], v2[k]) = {j, jj};
      j = jj;
    }
    if (~u1[k] and ~v1[k]) {
      ++res;
      if (v2[k] - v1[k] + u2[k] - u1[k] > n) {
        cout << "No\\n";
        return 0;
      }
      if (v1[k] <= u1[k]) {
        if (v2[k] <= u1[k]) {
          ++hh[0];
          --hh[u1[k] - v2[k] + 1];
          ++hh[u2[k] - v1[k]];
          --hh[n];
        } else {
          ++hh[u2[k] - v1[k]];
          --hh[u1[k] + n - v2[k] + 1];
        }
      } else {
        ++hh[max(0, u2[k] - v1[k])];
        --hh[u1[k] + n - v2[k] + 1];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (hh[i] == res) {
      cout << "Yes\\n";
      rotate(y.begin(), y.begin() + n - i, y.end());
      for (auto uu : y)
        cout << uu << " ";
      cout << endl;
      return 0;
    }
    hh[i + 1] += hh[i];
  }
  return 0;
}