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

#include <bits/stdc++.h>
#define LL __int128
#define ll long long
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scl(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld%lld", &a, &b)
#define ss(a) scanf("%s", a)
#define mem(a, b) memset(a, b, sizeof(a))
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define PII pair<int, int>
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
int a[maxn], b[maxn];
int main() {
  ios;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  reverse(b + 1, b + n + 1);
  bool ok = false;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i])
      continue;
    bool f = false;
    for (int j = 1; j <= n; j++) {
      if (a[i] != b[j] && a[j] != b[i]) {
        f = true;
        swap(b[i], b[j]);
        break;
      }
    }
    if (!f) {
      ok = 1;
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i <= n; i++) {
    if (i != 1)
      cout << " ";
    cout << b[i];
  }
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL __int128
#define ll long long
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scl(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld%lld", &a, &b)
#define ss(a) scanf("%s", a)
#define mem(a, b) memset(a, b, sizeof(a))
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define PII pair<int, int>
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
int a[maxn], b[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  reverse(b + 1, b + n + 1);
  bool ok = false;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i])
      continue;
    bool f = false;
    for (int j = 1; j <= n; j++) {
      if (a[i] != b[j] && a[j] != b[i]) {
        f = true;
        swap(b[i], b[j]);
        break;
      }
    }
    if (!f) {
      ok = 1;
      break;
    }
  }
  if (ok) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 1; i <= n; i++) {
      if (i != 1)
        cout << " ";
      cout << b[i];
    }
    cout << endl;
  }
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn], b[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> b[n - i + 1];
  int ok = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i])
      continue;
    int f = 0;
    for (int j = 1; j <= n; j++) {
      if (a[i] != b[j] && a[j] != b[i]) {
        f = 1;
        swap(b[i], b[j]);
        break;
      }
    }
    if (!f) {
      ok = 1;
      goto outer;
    }
  }
outer:;
  if (ok == 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 1; i <= n; i++) {
      if (i != 1)
        cout << " ";
      cout << b[i];
    }
    cout << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL __int128
#define ll long long
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scl(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld%lld", &a, &b)
#define ss(a) scanf("%s", a)
#define mem(a, b) memset(a, b, sizeof(a))
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define PII pair<int, int>
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
int a[maxn], b[maxn];
int main() {
  ios;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  reverse(b + 1, b + n + 1);
  int ok = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i])
      continue;
    int f = 0;
    for (int j = 1; j <= n; j++) {
      if (a[i] != b[j] && a[j] != b[i]) {
        f = 1;
        swap(b[i], b[j]);
        break;
      }
    }
    if (!f) {
      ok = 1;
      break;
    }
  }
  if (ok == 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 1; i <= n; i++) {
      if (i != 1)
        cout << " ";
      cout << b[i];
    }
    cout << endl;
  }
  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)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void solve() {
  int n;
  cin >> n;
  map<int, int> A, B, Merge;
  rep(i, n) {
    int a;
    cin >> a;
    ++A[a];
    ++Merge[a];
  }
  rep(i, n) {
    int b;
    cin >> b;
    ++B[b];
    ++Merge[b];
  }
  vector<P> cnter;
  for (auto x : Merge) {
    if (x.second > n) {
      cout << "No" << endl;
      return;
    }
    cnter.push_back(P(x.second, x.first));
  }
  sort(cnter.rbegin(), cnter.rend());
  vector<P> Left, Rights(n, P(-1, -1));
  int itr_zero = 0, itr_one = 0;
  rep(i, cnter.size()) {
    rep(q, A[cnter[i].second]) {
      if (Left.size() != n) {
        Left.push_back(P(cnter[i].second, 0));
      } else {
        while (Left[itr_one].second == 0)
          itr_one++;
        Rights[itr_one++] = P(cnter[i].second, 0);
      }
    }
    rep(q, B[cnter[i].second]) {
      if (Left.size() != n) {
        Left.push_back(P(cnter[i].second, 1));
      } else {
        while (Left[itr_zero].second == 1)
          itr_zero++;
        Rights[itr_zero++] = P(cnter[i].second, 1);
      }
    }
  }
  vector<P> ans;
  rep(i, Left.size()) {
    if (Left[i].second == 1)
      swap(Left[i], Rights[i]);
    ans.emplace_back(Left[i].first, Rights[i].first);
  }
  sort(ans.begin(), ans.end());
  cout << "Yes" << endl;
  rep(i, n) cout << ans[i].second << " ";
  cout << endl;
}

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


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  map<int, int> mp;
  vector<int> a(n), b(n);
  rep(i, n) {
    cin >> a[i];
    ++mp[a[i]];
  }
  rep(i, n) {
    cin >> b[i];
    ++mp[b[i]];
  }
  for (auto p : mp) {
    if (p.second > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  int l = 0, r = n - 1;
  while (l < r) {
    while (a[l] != b[l])
      ++l;
    while (a[r] != b[r])
      --r;
    swap(b[l], b[r]);
  }
  if (a[l] == b[l] && l == r) {
    rep(i, n) {
      if (a[i] != b[l] && a[l] != b[i]) {
        swap(b[i], b[l]);
        break;
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n) cout << b[i] << " ";
  cout << endl;
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

struct lr {
  int l;
  int r;
};

int main() {
  int n;
  cin >> n;
  VI a(n), b(n);
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> b[i];
  vector<lr> ap(n + 10, {-1, -1}), bp(n + 10, {-1, -1});
  ap[a[0]].l = 0;
  ap[a[n - 1]].r = n - 1;
  REP(i, n - 1) {
    if (a[i] != a[i + 1]) {
      ap[a[i]].r = i;
      ap[a[i + 1]].l = i + 1;
    }
  }
  bp[b[0]].l = 0;
  bp[b[n - 1]].r = n - 1;
  REP(i, n - 1) {
    if (b[i] != b[i + 1]) {
      bp[b[i]].r = i;
      bp[b[i + 1]].l = i + 1;
    }
  }
  VI s(n * 2, 0);
  for (int i = 1; i <= n; i++) {
    if (ap[i].l != -1 && bp[i].l != -1) {
      int bl = bp[i].r - bp[i].l + 1;
      int sl = ap[i].l - bl + 1 - bp[i].l;
      int sr = ap[i].r + bl - 1 - bp[i].r + 1;
      if (sr - sl >= n) {
        s[0]++;
      } else {
        if ((sl + 3 * n) % n < (sr + 3 * n) % n) {
          s[(sl + 3 * n) % n]++;
          s[(sr + 3 * n) % n]--;
        } else {
          s[(sl + 3 * n) % n]++;
          s[0]++;
          s[(sr + 3 * n) % n]--;
        }
      }
    }
  }
  int p = 0;
  REP(i, n) {
    p += s[i];
    if (p == 0) {
      cout << "Yes" << endl;
      REP(j, n)
      cout << b[(j - i + n) % n] << " ";
      cout << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

struct lr {
  int l;
  int r;
};

int main() {
  int n;
  cin >> n;
  VI a(n), b(n);
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> b[i];
  vector<lr> ap(n + 10, {-1, -1}), bp(n + 10, {-1, -1});
  ap[a[0]].l = 0;
  ap[a[n - 1]].r = n - 1;
  REP(i, n - 1) {
    if (a[i] != a[i + 1]) {
      ap[a[i]].r = i;
      ap[a[i + 1]].l = i + 1;
    }
  }
  bp[b[0]].l = 0;
  bp[b[n - 1]].r = n - 1;
  REP(i, n - 1) {
    if (b[i] != b[i + 1]) {
      bp[b[i]].r = i;
      bp[b[i + 1]].l = i + 1;
    }
  }
  VI s(n * 2, 0);
  for (int i = 1; i <= n; i++) {
    if (ap[i].l != -1 && bp[i].l != -1) {
      int bl = bp[i].r - bp[i].l + 1;
      int sl = ap[i].l - bl + 1 - bp[i].l;
      int sr = ap[i].r + bl - 1 - bp[i].r + 1;
      if (sl < 0) {
        if (sl + n > 0)
          s[(sl + 3 * n) % n]++;
        else
          s[0]++;
        s[0]++;
      } else {
        s[sl]++;
      }
      if (sr >= n) {
        s[0]++;
        if (sr - 2 * n < 0)
          s[sr % n]--;
      } else {
        s[sr]--;
      }
    }
  }
  int p = 0;
  REP(i, n) {
    p += s[i];
    if (p == 0) {
      cout << "Yes" << endl;
      REP(j, n)
      cout << b[(j - i + n) % n] << " ";
      cout << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const double eps = 1e-6, pi = acos(-1);
const int N = 2e5 + 5, M = 1e9 + 7, SEGM = 4 * N, OO = 0x3f3f3f3f;
int t, n, m, k;
int a[N];
int b[N];

int main() {
  // freopen("myfile.txt","w",stdout);
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  for (int i = n - 1; i > -1; --i)
    scanf("%d", b + i);
  int l = n, r = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] == b[i]) {
      l = i;
      break;
    }
  }
  for (int i = n - 1; i > -1; --i) {
    if (a[i] == b[i]) {
      r = i;
      break;
    }
  }
  for (int i = 0; i < n; ++i)
    if (l <= r && b[l] != a[i] && b[l] != b[i])
      swap(b[i], b[l++]);
  if (l <= r) {
    printf("No\\n");
  } else {
    printf("Yes\\n");
    for (int i = 0; i < n; ++i)
      printf("%d%c", b[i], " \\n"[i + 1 == n]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const double eps = 1e-6, pi = acos(-1);
const int N = 2e5 + 5, M = 1e9 + 7, SEGM = 4 * N, OO = 0x3f3f3f3f;
int t, n, m, k;
int a[N];
int b[N];

bool cool() {
  for (int i = 0; i < n; ++i)
    if (a[i] == b[i])
      return 0;
  return 1;
}

bool change(int idx) {
  if (a[idx] != b[idx])
    return 1;
  bool done = 0;
  for (int i = idx + 1; i < n && !done; ++i) {
    if (b[i] != b[idx] && a[i] != b[idx])
      swap(b[i], b[idx]), done = 1;
  }
  for (int i = 0; i < idx && !done; ++i) {
    if (b[i] != b[idx] && a[i] != b[idx])
      swap(b[i], b[idx]), done = 1;
  }
  return done;
}

int main() {
  // freopen("myfile.txt","w",stdout);
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  for (int i = n - 1; i > -1; --i)
    scanf("%d", b + i);
  if (cool()) {
    printf("Yes\\n");
    for (int i = 0; i < n; ++i)
      printf("%d%c", b[i], " \\n"[i + 1 == n]);
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    if (!change(i)) {
      printf("No\\n");
      return 0;
    }
  }
  printf("Yes\\n");
  for (int i = 0; i < n; ++i)
    printf("%d%c", b[i], " \\n"[i + 1 == n]);
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int N;
  cin >> N;
  int A[200010], B[200010];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++)
    cin >> B[i];
  int cnt[200010] = {};
  for (int i = 0; i < N; i++) {
    cnt[A[i]]++;
    cnt[B[i]]++;
  }
  for (int i = 1; i <= N; i++) {
    if (cnt[i] > N) {
      cout << "No"
           << "\\n";
      return 0;
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 0; i < (N + 1) / 2; i++)
    swap(B[i], B[N - 1 - i]);
  vector<int> overlap;
  vector<int> not_overlap;
  int num = -1;
  for (int i = 0; i < N; i++) {
    if (A[i] == B[i]) {
      overlap.push_back(i);
      num = A[i];
    }
  }
  for (int i = 0; i < N; i++) {
    if (A[i] != num && B[i] != num)
      not_overlap.push_back(i);
  }
  for (int i = 0; i < (int)overlap.size(); i++) {
    swap(B[overlap.at(i)], B[not_overlap.at(i)]);
  }
  for (int i = 0; i < N; i++)
    cout << B[i] << " ";
  cout << "\\n";
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int N;
  cin >> N;
  int A[200010], B[200010];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++)
    cin >> B[i];
  int cnt1[200010] = {};
  int cnt2[200010] = {};
  for (int i = 0; i < N; i++) {
    cnt1[A[i]]++;
    cnt2[B[i]]++;
  }
  for (int i = 1; i <= N; i++) {
    if (cnt1[i] + cnt2[i] > N) {
      cout << "No"
           << "\\n";
      return 0;
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 0; i <= N / 2; i++)
    swap(B[i], B[N - 1 - i]);
  int l = -1;
  int r = -1;
  bool start = false;
  vector<int> overlap;
  vector<int> not_overlap;
  for (int i = 0; i < N; i++) {
    if (A[i] == B[i])
      overlap.push_back(i);
    else
      not_overlap.push_back(i);
  }
  for (int i = 0; i < (int)overlap.size(); i++) {
    swap(B[overlap.at(i)], B[not_overlap.at(i)]);
  }
  for (int i = 0; i < N; i++)
    cout << B[i] << " ";
  cout << "\\n";
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define INF 1000000000000 // 10^12:∞
#define MOD 1000000007    // 10^9+7:合同式の法
#define MAXR 100000       // 10^5:配列の最大のrange

#define PB push_back // 挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}

template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

void solve() {
  ll N;
  cin >> N;
  vector<ll> c(N + 1, 0), d(N + 1, 0), A(N), B(N);
  REP(i, N) {
    cin >> A[i];
    c[A[i]]++;
  }
  REP(i, N) {
    cin >> B[i];
    d[B[i]]++;
  }

  FOR(i, 1, N) {
    if (c[i] + d[i] > N) {
      cout << "No" << endl;
      return;
    }
    c[i] += c[i - 1];
    d[i] += d[i - 1];
  }

  ll x = 0;
  FOR(i, 1, N) { chmax(x, c[i] - d[i - 1]); }
  cout << "Yes" << endl;
  REP(i, N) {
    if (i)
      cout << " ";
    cout << B[(i + N - x) % N];
  }
  cout << endl;
}

int main() {
  solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define INF 1000000000000 // 10^12:∞
#define MOD 1000000007    // 10^9+7:合同式の法
#define MAXR 100000       // 10^5:配列の最大のrange

#define PB push_back // 挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}

template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

void solve() {
  ll N;
  cin >> N;
  vector<ll> cnt(N + 1), cnt_b(N + 1), A(N), B(N), ans(N);
  REP(i, N) {
    cin >> A[i];
    cnt[A[i]]++;
  }
  REP(i, N) {
    cin >> B[i];
    cnt[B[i]]++;
    cnt_b[B[i]]++;
  }

  REP(i, N + 1) {
    if (cnt[i] > N) {
      cout << "No" << endl;
      return;
    }
  }

  REP(i, N) {
    ll j = N - i - 1;
    while (A[i] == B[j]) {
      j--;
    }
    ans[i] = B[j];
    swap(B[N - i - 1], B[j]);
  }
  REP(i, N) {
    cout << ans[i];
    if (i < N - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
}

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

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

// THINK: INT vs LONG LONG?

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

  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 start = -1, fin = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] == b[i]) {
      if (start == -1)
        start = i;
      fin = i;
    }
  }
  if (start != -1) {
    for (int i = start; i <= fin; ++i) {
      assert(b[i] == a[i]);
    }
  }
  bool bad = 0;
  if (start != -1) {
    int j = start;
    for (int i = 0; j <= fin; ++i) {
      if (i == start)
        i = fin + 1;
      if (i >= n) {
        bad = 1;
        break;
      }
      if (a[i] != b[j] && b[j] != b[i]) {
        swap(b[i], b[j]);
        j++;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (a[i] == b[i])
      bad = 1;
  }
  if (bad) {
    cout << "No" << '\\n';
  } else {
    cout << "Yes" << '\\n';
    for (int i = 0; i < n; ++i) {
      if (i)
        cout << " ";
      cout << b[i];
    }
    cout << '\\n';
  }

  return 0;
}

Error: WA

Fixed submission:

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

// THINK: INT vs LONG LONG?

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

  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 start = -1;
  int fin = -1;
  for (int i = 0; i < n; ++i) {
    if (b[i] == a[i]) {
      if (start == -1)
        start = i;
      fin = i;
    }
  }
  bool bad = 0;
  if (start != -1) {
    for (int i = 0, j = start; j <= fin && j >= 0; ++j) {
      if (i == n || i == fin) {
        bad = 1;
        break;
      }
      swap(b[i], b[j]);
      if (i == start - 1) {
        i = n - 1;
      } else {
        if (i < start - 1)
          i++;
        else
          i--;
      }
    }
    for (int i = 0; i < n; ++i) {
      if (a[i] == b[i])
        bad = 1;
    }
  }
  if (bad) {
    cout << "No" << '\\n';
  } else {
    cout << "Yes" << '\\n';
    for (int i = 0; i < n; ++i) {
      if (i)
        cout << " ";
      cout << b[i];
    }
    cout << '\\n';
  }

  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void Main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[n - 1 - i];
    m[b[n - 1 - i]]++;
  }
  for (auto i : m) {
    if (i.second > n) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    while (a[i] == b[i]) {
      int r = rand() % n;
      if (a[r] != b[i] && b[r] != b[i]) {
        swap(b[i], b[r]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << b[i];
  }
  cout << endl;
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

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

void Main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[n - 1 - i];
    m[b[i]]++;
  }
  for (auto i : m) {
    if (i.second > n) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    while (a[i] == b[i]) {
      int r = rand() % n;
      if (a[r] != b[i] && b[r] != b[i]) {
        swap(b[i], b[r]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << b[i];
  }
  cout << endl;
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}