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

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int n, x;
vector<int> a, b;
bool judge() {
  for (int i = 0; i < n; i++) {
    if (abs(a[i] - b[i]) == 0)
      return 0;
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &x), a.push_back(x);
  for (int i = 0; i < n; i++)
    scanf("%d", &x), b.push_back(x);
  int m = n;
  while (m--) {
    if (judge()) {
      cout << "Yes" << endl;
      for (int i = 0; i < n; i++)
        cout << b[i] << " ";
      exit(0);
    }
    int u = b.front();
    b.erase(b.begin());
    b.push_back(u);
  }
  cout << "No";
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <queue>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  vector<int> Bnum(N + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
    Bnum[B[i]]++;
  }
  int r = 0;
  for (int l = 0; l < N; l++) {
    while (A[l] == B[r] && r < N - 1)
      r++;
    swap(B[l], B[r]);
    if (l == r)
      r++;
  }
  int l = N - 1;
  for (int r = N - 1; r >= 0; r--) {
    while (A[r] == B[l] && l > 0)
      l--;
    swap(B[r], B[l]);
    if (l == r)
      l--;
  }
  for (int i = 0; i < N; i++) {
    if (A[i] == B[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  /*priority_queue<pair<int, int>>que;
  for (int i = 1; i <= N; i ++) {
    que.push(make_pair(Bnum[i], i));
  }
  for (int i = 0; i < N; i ++) {
    int pre, kj_;
    tie(pre, kj_) = que.top();
    que.pop();
    if (kj_ == A[i]) {
      int now, kj;
      tie(now, kj) = que.top();
      que.pop();
      if (now == 0) {
        cout << "No" << endl;
        return 0;
      }
      que.push(make_pair(pre, kj_));
      pre = now;
      kj_ = kj;
    }
    B[i] = kj_;
    Bnum[kj_] --;
    que.push(make_pair(pre - 1, kj_));
    cout << kj_ << endl;
  }*/
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << B[i];
    if (i < N - 1)
      cout << ' ';
  }
  cout << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  vector<int> Anum(N + 1, 0);
  vector<int> Bnum(N + 1, 0);
  vector<int> nex(N + 2);
  vector<int> bac(N + 2);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    Anum[A[i]]++;
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
    Bnum[B[i]]++;
  }
  /*int ma = -1;
  nex[N + 1] = -1;
  bac[0] = -1;
  for (int i = 1; i <= N; i ++) nex[i] = i + 1;
  for (int i = N; i > 0; i --) bac[i] = i - 1;
  for (int i = 1; i <= N; i ++) {
    if (Bnum[i] == 0) {
      nex[bac[i]] = nex[i];
      bac[nex[i]] = bac[i];
    }
  }*/
  priority_queue<pair<int, int>> que;
  for (int i = 1; i <= N; i++) {
    que.push(make_pair(Bnum[i], i));
  }
  for (int i = 0; i < N; i++) {
    int pre, kj_, now, kj;
    tie(pre, kj_) = que.top();
    que.pop();
    if (kj_ == A[i]) {
      tie(now, kj) = que.top();
      que.pop();
      if (now == 0) {
        cout << "No" << endl;
        return 0;
      }
      que.push(make_pair(pre, kj_));
      pre = now;
      kj_ = kj;
    }
    B[i] = kj_;
    Bnum[kj_]--;
    que.push(make_pair(pre - 1, kj_));
  }
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << B[i];
    if (i < N - 1)
      cout << ' ';
  }
  cout << endl;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

// This Code was made by Chinese_zjc_.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #include<windows.h>
#define int long long
#define double long double
using namespace std;
const double PI = acos(-1);
const double eps = 0.0000000001;
const int INF = 0x3fffffffffffffff;
int n, tmp, A[200005], B[200005], a[200005], b[200005], cnt, Alst[200005],
    Bbeg[200005], maxlen;
signed main() {
  ios::sync_with_stdio(false);
  cin >> n;
  fill(Bbeg + 1, Bbeg + 1 + n, INF);
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
    Alst[A[i]] = i;
    ++a[A[i]];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> B[i];
    if (Bbeg[B[i]] == INF) {
      Bbeg[B[i]] = i;
    }
    ++b[B[i]];
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] + b[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  A[0] = A[n];
  B[0] = B[n];
  for (int i = 1; i <= n; ++i) {
    maxlen = max(maxlen, Alst[i] - Bbeg[i] + 1);
  }
  for (int i = 1; i <= n; ++i) {
    cout << B[(i - maxlen + n) % n] << ' ';
  }
  cout << endl;
  return 0;
}

Error: WA

Fixed submission:

// This Code was made by Chinese_zjc_.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #include<windows.h>
#define int long long
#define double long double
using namespace std;
const double PI = acos(-1);
const double eps = 0.0000000001;
const int INF = 0x3fffffffffffffff;
int n, tmp, A[200005], B[200005], a[200005], b[200005], cnt;
struct L {
  int v, p;
  bool operator<(const L &A) const { return v < A.v; }
} c[200005], now;
priority_queue<L> que;
signed main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
    ++a[A[i]];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> B[i];
    ++b[B[i]];
  }
  for (int i = 1; i <= n; ++i) {
    c[i] = {b[i], i};
    que.push(c[i]);
    if (a[i] + b[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i <= n; ++i) {
    while (!que.empty() && que.top().v == 0) {
      que.pop();
    }
    if (now.v == 0) {
      now = que.top();
      que.pop();
    }
    if (now.p == A[i]) {
      L gg = que.top();
      cout << gg.p << ' ';
      --gg.v;
      que.pop();
      que.push(gg);
    } else {
      cout << now.p << ' ';
      --now.v;
    }
  }
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define endl '\\n'
#define Endl '\\n'
#define fi first
#define ii pair<int, int>
#define iil pair<ll, ll>
#define se second
#define ld long double
#define mod 1000000007
#define popcount __builtin_popcountll
#define x0 zxcxzcsacasd
#define x1 ghfghujfgfg
#define y0 olkjhdnncvcv
#define y1 bhvgrewm345345435
#define debug(x) std::cerr << #x << ": " << (x) << '\\n'
#define all(x) x.begin(), x.end()
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), amid(n);
  vector<int> cnt(n + 1, 0);
  for (auto &x : a)
    scanf("%d", &x), cnt[x]++;
  for (auto &x : b)
    scanf("%d", &x), cnt[x]++;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  reverse(all(b));
  int l = 0, r = n - 1;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      if (b[l] != a[i] && a[l] != a[i]) {
        swap(b[l], b[i]);
        l++;
      } else {
        swap(b[r], b[i]);
        r--;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    assert(a[i] != b[i]);
    cout << b[i] << " ";
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define endl '\\n'
#define Endl '\\n'
#define fi first
#define ii pair<int, int>
#define iil pair<ll, ll>
#define se second
#define ld long double
#define mod 1000000007
#define popcount __builtin_popcountll
#define x0 zxcxzcsacasd
#define x1 ghfghujfgfg
#define y0 olkjhdnncvcv
#define y1 bhvgrewm345345435
#define debug(x) std::cerr << #x << ": " << (x) << '\\n'
#define all(x) x.begin(), x.end()
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), amid(n);
  vector<int> cnt(n + 1, 0);
  for (auto &x : a)
    scanf("%d", &x), cnt[x]++;
  for (auto &x : b)
    scanf("%d", &x), cnt[x]++;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  reverse(all(b));
  int l = 0, r = n - 1;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      if (b[l] != a[i]) {
        swap(b[l], b[i]);
        l++;
      } else if (b[r] != a[i]) {
        swap(b[r], b[i]);
        r--;
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    assert(a[i] != b[i]);
    cout << b[i] << " ";
  }
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 200020
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, a[N], b[N], d, cnt[N];
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]];
  }
  int pa = 1, pb = 1;
  for (int i = 1; i <= n; ++i) {
    while (a[pa] == i)
      ++pa;
    d = max(d, pa - pb);
    while (b[pb] == i)
      ++pb;
  }
  rotate(b + 1, b + n + 1 - d, b + n + 1);
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i])
      return !printf("No\\n");
  }
  printf("Yes\\n");
  for (int i = 1; i <= n; ++i) {
    printf("%d ", b[i]);
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 200020
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, a[N], b[N], d;
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
  }
  for (int i = 1; i <= n; ++i) {
    b[i] = read();
  }
  int pa = 1, pb = 1;
  for (int i = 1; i <= n; ++i) {
    while (a[pa] == i)
      ++pa;
    d = max(d, pa - pb);
    while (b[pb] == i)
      ++pb;
  }
  rotate(b + 1, b + n + 1 - d, b + n + 1);
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i])
      return printf("No\\n");
  }
  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;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

int n;

int main() {
  cin >> n;
  vector<int> avec(n, 0);
  vector<int> bvec(n, 0);
  for (int i = 0; i < n; ++i)
    cin >> avec[i];
  for (int i = 0; i < n; ++i)
    cin >> bvec[i];

  map<int, int> acnt;
  map<int, int> bcnt;
  for (int i = 0; i < n; ++i) {
    acnt[avec[i]]++;
    bcnt[bvec[i]]++;
  }

  for (int i = 0; i < n; ++i) {
    if (n - acnt[avec[i]] < bcnt[avec[i]]) {
      cout << "No" << endl;
      return 0;
    }
  }

  sort(bvec.begin(), bvec.end(), greater<int>());
  for (int i = 0; i < n; ++i) {
    if (avec[i] == bvec[i]) {
      for (int j = 0; j < i; ++j) {
        if (avec[i] != bvec[j] && avec[j] != bvec[i]) {
          swap(bvec[i], bvec[j]);
          break;
        }
      }
    }
  }

  bool flg = true;
  for (int i = 0; i < n; ++i) {
    if (avec[i] == bvec[i]) {
      for (int j = 0; j < n; ++j) {
        if (i == j)
          continue;
        if (avec[i] != bvec[j] && avec[j] != bvec[i]) {
          swap(bvec[i], bvec[j]);
          break;
        }
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    if (avec[i] == bvec[i])
      flg = false;
  }

  if (flg) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; ++i) {
      if (i == n - 1)
        cout << bvec[i] << endl;
      else
        cout << bvec[i] << " ";
    }
  } else
    cout << "No" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

int n;

int main() {
  cin >> n;
  vector<int> avec(n, 0);
  vector<int> bvec(n, 0);
  for (int i = 0; i < n; ++i)
    cin >> avec[i];
  for (int i = 0; i < n; ++i)
    cin >> bvec[i];

  sort(bvec.begin(), bvec.end(), greater<int>());
  for (int i = 0; i < n; ++i) {
    if (avec[i] == bvec[i]) {
      for (int j = 0; j < i; ++j) {
        if (avec[i] != bvec[j])
          swap(bvec[i], bvec[j]);
      }
    }
  }

  bool flg = true;
  for (int i = 0; i < n; ++i) {
    if (avec[i] == bvec[i])
      flg = false;
  }

  if (flg) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; ++i) {
      if (i == n - 1)
        cout << bvec[i] << endl;
      else
        cout << bvec[i] << " ";
    }
  } else
    cout << "No" << endl;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> ii;

int main() {
  int n;
  scanf("%d", &n);
  vector<ii> a(n, ii());
  vector<int> b(n, 0);
  map<int, int> ma, mb;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i].first);
    a[i].second = i;
    if (ma.count(a[i].first))
      ma[a[i].first]++;
    else
      ma[a[i].first] = 1;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &b[i]);
    if (mb.count(b[i]))
      mb[b[i]]++;
    else
      mb[b[i]] = 1;
  }
  for (auto it = mb.begin(), end = mb.end(); it != end; ++it) {
    if (ma.count(it->first) && ma[it->first] + (it->second) > n) {
      puts("No");
      exit(0);
    }
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int p = 0, c; 1; ++p) {
    while (p < n && a[0].first == b[p])
      ++p;
    if (p == n) {
      puts("No");
      break;
    }
    for (c = 0; c < n; ++c) {
      if (a[c].first == b[(p + c) % n])
        break;
    }
    if (c == n) {
      puts("Yes");
      vector<int> ans(n, 0);
      for (int i = 0; i < n; ++i)
        ans[a[i].second] = b[(p + i) % n];
      printf("%d", ans[0]);
      for (int i = 1; i < n; ++i)
        printf(" %d", ans[i]);
      puts("");
      break;
    }
  }
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> ii;

int main() {
  int n;
  scanf("%d", &n);
  vector<ii> a(n, ii());
  vector<int> b(n, 0);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i].first);
    a[i].second = i;
  }
  for (int i = 0; i < n; ++i)
    scanf("%d", &b[i]);
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int p = 0, c; 1; ++p) {
    while (p < n && a[0].first == b[p])
      ++p;
    if (p == n) {
      puts("No");
      break;
    }
    for (c = 0; c < n; ++c) {
      if (a[c].first == b[(p + c) % n])
        break;
    }
    if (c == n) {
      puts("Yes");
      vector<int> ans(n, 0);
      for (int i = 0; i < n; ++i)
        ans[a[i].second] = b[(p + i) % n];
      printf("%d", ans[0]);
      for (int i = 1; i < n; ++i)
        printf(" %d", ans[i]);
      puts("");
      break;
    }
  }
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
#define MOD 1000000007

int main() {
  int n;
  cin >> n;
  map<int, int> ma, mb;
  vi aa(n), bb(n);
  rep(i, n) {
    int a;
    cin >> a;
    ma[a]++;
    aa[i] = a;
  }
  queue<int> que;
  rep(i, n) {
    int b;
    cin >> b;
    mb[b]++;
    bb[i] = b;
  }
  for (auto x : ma) {
    if (x.second + mb[x.first] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  vi ans;
  cout << "Yes" << endl;
  int i = 0;
  reverse(bb.begin(), bb.end());
  rep(i, n) {
    if (i % 3 == 0)
      que.push(bb[i]);
  }
  rep(i, n) {
    if (i % 3 == 1)
      que.push(bb[i]);
  }
  rep(i, n) {
    if (i % 3 == 2)
      que.push(bb[i]);
  }
  while (!que.empty()) {
    if (que.front() != aa[i]) {
      ans.push_back(que.front());
      que.pop();
      i++;
    } else {
      que.push(que.front());
      que.pop();
    }
  }
  for (int x : ans)
    cout << x << " ";
  cout << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
#define MOD 1000000007

int main() {
  int n;
  cin >> n;
  map<int, int> ma, mb;
  vi aa(n), bb(n);
  rep(i, n) {
    int a;
    cin >> a;
    ma[a]++;
    aa[i] = a;
  }
  queue<int> que;
  rep(i, n) {
    int b;
    cin >> b;
    mb[b]++;
    bb[i] = b;
  }
  for (auto x : ma) {
    if (x.second + mb[x.first] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  vi ans;
  cout << "Yes" << endl;
  int i = 0;
  reverse(bb.begin(), bb.end());
  rep(i, n) { que.push(bb[i]); }
  while (!que.empty()) {
    if (que.front() != aa[i]) {
      ans.push_back(que.front());
      que.pop();
      i++;
    } else {
      que.push(que.front());
      que.pop();
    }
  }
  for (int x : ans)
    cout << x << " ";
  cout << endl;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define REP(i, start, end) for (ll i = start, i##Len = (end); i < i##Len; ++i)
#define REPR(i, start, end) for (ll i = start, i##Len = (end); i > i##Len; --i)
using ll = long long;
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll n;
  cin >> n;
  vector<ll> a(n), b(n), a_cnt(n + 1), b_cnt(n + 1);
  REP(i, 0, n) {
    cin >> a[i];
    a_cnt[a[i]]++;
  }
  REP(i, 0, n) {
    cin >> b[i];
    b_cnt[b[i]]++;
  }

  vector<int> idx(n + 1);
  iota(idx.begin(), idx.end(), 0);
  sort(idx.begin(), idx.end(),
       [&](int i, int j) { return a_cnt[i] > a_cnt[j]; });

  deque<pair<int, int>> dq;
  REP(i, 0, n + 1) {
    if (b_cnt[idx[i]] > 0)
      dq.emplace_back(idx[i], b_cnt[idx[i]]);
  }

  map<int, vector<int>> mp;
  REP(i, 0, n + 1) {
    if (a_cnt[idx[i]] == 0)
      break;
    int x = idx[i];
    if (!dq.empty() && dq.front().first == x) {
      dq.push_back(dq.front());
      dq.pop_front();
    }
    REP(_, 0, a_cnt[x]) {
      if (dq.front().first == x) {
        cout << "No" << endl;
        return 0;
      }
      mp[x].emplace_back(dq.front().first);
      dq.front().second--;
      if (dq.front().second == 0)
        dq.pop_front();
    }
  }

  cout << "Yes\\n";
  REP(i, 0, n) {
    cout << mp[a[i]].back() << " ";
    mp[a[i]].pop_back();
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define REP(i, start, end) for (ll i = start, i##Len = (end); i < i##Len; ++i)
#define REPR(i, start, end) for (ll i = start, i##Len = (end); i > i##Len; --i)
using ll = long long;
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll n;
  cin >> n;
  vector<ll> a(n), b(n), a_cnt(n + 1), b_cnt(n + 1);
  REP(i, 0, n) {
    cin >> a[i];
    a_cnt[a[i]]++;
  }
  REP(i, 0, n) {
    cin >> b[i];
    b_cnt[b[i]]++;
  }

  vector<int> idx(n + 1);
  iota(idx.begin(), idx.end(), 0);
  sort(idx.begin(), idx.end(),
       [&](int i, int j) { return a_cnt[i] > a_cnt[j]; });

  deque<pair<int, int>> dq;
  REP(i, 0, n + 1) {
    if (b_cnt[idx[i]] > 0)
      dq.emplace_back(idx[i], b_cnt[idx[i]]);
  }

  map<int, vector<int>> mp;
  REP(i, 0, n + 1) {
    if (a_cnt[idx[i]] == 0)
      break;
    int x = idx[i];
    while (!dq.empty() && dq.front().first == x) {
      dq.push_back(dq.front());
      dq.pop_front();
    }
    REP(_, 0, a_cnt[x]) {
      if (dq.front().first == x) {
        cout << "No" << endl;
        return 0;
      }
      mp[x].emplace_back(dq.front().first);
      dq.front().second--;
      if (dq.front().second == 0)
        dq.pop_front();
    }
  }

  cout << "Yes\\n";
  REP(i, 0, n) {
    cout << mp[a[i]].back() << " ";
    mp[a[i]].pop_back();
  }
  cout << endl;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

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

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (auto &i : a)
    cin >> i;
  for (auto &i : b)
    cin >> i;

  reverse(b.begin(), b.end());
  int l = -1, r = -2;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      if (l == -1)
        l = r = i;
      else
        r++;
    }
  }

  if (l == -1) {
    cout << "Yes\\n";
    for (auto i : b)
      cout << i << " ";
    cout << '\\n';
    return 0;
  }

  int p = lower_bound(a.begin(), a.end(), a[l]) - a.begin() - 1;
  int q = upper_bound(a.begin(), a.end(), a[l]) - a.begin();
  int lo = 0, hi = n - 1, k = n;
  while (lo <= hi) {
    int mid = (hi - lo) / 2 + lo;
    if (b[mid] < a[l]) {
      k = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  q = max(q, k);
  k = -1;
  lo = 0, hi = n - 1;
  while (lo <= hi) {
    int mid = (hi - lo) / 2 + lo;
    if (b[mid] > a[l]) {
      k = mid;
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }
  p = min(p, k);
  int idx = 0;
  if (p < 0)
    idx = q;
  for (int i = l; i <= r; i++) {
    if (idx == n) {
      cout << "No\\n";
      return 0;
    }
    swap(b[i], b[idx]);
    idx++;
    if (idx == p + 1)
      idx = q;
  }

  cout << "Yes\\n";
  for (auto i : b)
    cout << i << " ";
  cout << '\\n';
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

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

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (auto &i : a)
    cin >> i;
  for (auto &i : b)
    cin >> i;

  reverse(b.begin(), b.end());
  int l = -1, r = -2;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      if (l == -1)
        l = r = i;
      else
        r++;
    }
  }

  if (l == -1) {
    cout << "Yes\\n";
    for (auto i : b)
      cout << i << " ";
    cout << '\\n';
    return 0;
  }

  int p = lower_bound(a.begin(), a.end(), a[l]) - a.begin() - 1;
  int q = upper_bound(a.begin(), a.end(), a[l]) - a.begin();
  q = max(q, r + 1);
  p = min(p, l - 1);
  int idx = 0;
  if (p <= 0)
    idx = q + 1;
  for (int i = l; i <= r; i++) {
    if (idx == n) {
      cout << "No\\n";
      return 0;
    }
    swap(b[i], b[idx]);
    idx++;
    if (idx == p)
      idx = q + 1;
  }

  cout << "Yes\\n";
  for (auto i : b)
    cout << i << " ";
  cout << '\\n';
}