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

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

int n;
int a[200005], b[200005];
int cnta[200005], cntb[200005];
vector<int> invalid, unused;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; i++)
    scanf("%d", &b[i]);
  for (int i = 0; i < n; i++)
    cnta[a[i]]++;
  for (int i = 0; i < n; i++)
    cntb[b[i]]++;
  for (int i = 1; i <= n; i++) {
    if (cnta[i] + cntb[i] > n) {
      printf("No\\n");
      return 0;
    }
  }
  reverse(b, b + n);
  int same = -1;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      invalid.push_back(i);
      same = a[i];
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (cnt >= invalid.size())
      break;
    if (a[i] != b[i] && a[i] != same && b[i] != same) {
      cnt++;
      unused.push_back(i);
    }
  }
  for (int i = 0; i < invalid.size(); i++) {
    swap(b[invalid[i]], b[unused[i]]);
  }
  printf("Yes\\n");
  for (int i = 0; i < n; i++)
    printf("%d ", b[i]);
  printf("\\n");
  return 0;
}


Error: WA

Fixed submission:

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

int n;
int a[200005], b[200005];
int cnta[200005], cntb[200005];
vector<int> invalid, unused;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; i++)
    scanf("%d", &b[i]);
  for (int i = 0; i < n; i++)
    cnta[a[i]];
  for (int i = 0; i < n; i++)
    cntb[b[i]];
  for (int i = 1; i <= n; i++) {
    if (cnta[i] + cntb[i] > n) {
      printf("No\\n");
      return 0;
    }
  }
  reverse(b, b + n);
  int same = -1;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      invalid.push_back(i);
      same = a[i];
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (cnt >= invalid.size())
      break;
    if (a[i] != b[i] && a[i] != same && b[i] != same) {
      cnt++;
      unused.push_back(i);
    }
  }
  for (int i = 0; i < invalid.size(); i++) {
    swap(b[invalid[i]], b[unused[i]]);
  }
  printf("Yes\\n");
  for (int i = 0; i < n; i++)
    printf("%d ", b[i]);
  printf("\\n");
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> cnt(n + 1), a(n), b(n);
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  rep(i, n) {
    cin >> b[i];
    cnt[b[i]]++;
  }
  rep(i, n) {
    if (cnt[i + 1] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  int j = 0;
  rep(i, n) {
    if (a[i] == b[i]) {
      for (; j < n; j++) {
        if (a[j] != a[i] && b[j] != a[i]) {
          swap(b[i], b[j]);
          break;
        }
      }
    }
    if (i != n - 1) {
      if (a[i] != a[i + 1])
        j = 0;
    }
  }
  cout << "Yes" << endl;
  rep(i, n) cout << b[i] << ' ';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using ll = long long;

const ll mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<int> cnt(n + 1), a(n), b(n);
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  rep(i, n) {
    cin >> b[i];
    cnt[b[i]]++;
  }
  rep(i, n) {
    if (cnt[i + 1] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (a[i] == b[i]) {
      rep(j, n) {
        if (a[j] != b[i] && a[i] != b[j] && i != j) {
          swap(b[i], b[j]);
          j = n;
        }
      }
    }
  }
  rep(i, n) { cout << b[i] << ' '; }
  return 0;
}
/*
ll s;
cin >> s;
ll ans = 0;
if (s < 6) {
        ans = s / 3;
        cout << ans << endl;
        return 0;
}
for (ll i = 1; i <= s / 3; i++) {
        ll n = s - (i*3);

}

cout << ans << endl;
*/
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N], b[N], n;
inline int read() {
  int tot = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    tot = tot * 10 + c - '0';
    c = getchar();
  }
  return tot * f;
}
signed main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++)
    b[i] = read();
  reverse(b + 1, b + 1 + n);
  int l = 0, r, t, L, R;
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) {
      if (l == 0)
        l = i, r = i, t = b[i];
      else
        r++;
    } else if (l != 0)
      break;
  }
  if (l == 0) {
    printf("Yes\\n");
    for (int i = 1; i <= n; i++)
      printf("%d ", b[i]);
    return puts(""), 0;
  }
  // l~r为交集
  // L~R为并集
  for (int i = 1; i <= n; i++)
    if (a[i] == t || b[i] == t) {
      L = i - 1;
      break;
    }
  for (int i = n; i >= 1; i--)
    if (a[i] == t || b[i] == t) {
      R = i + 1;
      break;
    }
  // cout<<t<<" "<<L<<" "<<R<<endl;
  if (n - (R - L) < R - L)
    return puts("No"), 0;
  int pos = 1;
  for (int i = l; i <= r; i++) {
    if (pos == L + 1)
      pos = R;
    swap(b[i], b[pos]);
    pos++;
  }
  printf("Yes\\n");
  for (int i = 1; i <= n; i++)
    printf("%d ", b[i]);
  return puts(""), 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, a[N], b[N];
int asum[N], bsum[N], ans[N];
inline int read() {
  int tot = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    tot = tot * 10 + c - '0';
    c = getchar();
  }
  return tot * f;
}
signed main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++)
    b[i] = read();
  for (int i = 1; i <= n; i++)
    asum[a[i]]++, bsum[b[i]]++;
  int pos = n;
  for (int i = 1; i <= n; i++) {
    while (bsum[pos] == 0 && pos > 0)
      pos--;
    if (pos == 0) {
      while (1)
        ;
      return printf("No\\n"), 0;
    }
    if (pos == a[i]) {
      int lapos = pos;
      while ((!bsum[lapos] || lapos == a[i]) && lapos > 0)
        lapos--;
      if (lapos == 0) {
        while (1)
          ;
        return printf("No\\n"), 0;
      }
      ans[i] = lapos, bsum[lapos]--;
    } else
      ans[i] = pos, bsum[pos]--;
  }
  printf("Yes\\n");
  for (int i = 1; i <= n; i++)
    printf("%d ", ans[i]);
  puts("");
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
/*
Ö±½Ó°ÑAB²¢ÆðÀ´£¬ÉèÖÃÍ·Î²Ö¸Õë
´Ó×óÍùÓÒ±éÀú£¬Óöµ½²»ºÏÀíµÄ¾Í´ÓÍ·Î²½»»»¼´¿É
Ò»Ð©Ð¡ÓÅ»¯£º
¸ù¾Ý³éÌëÔ­ÀíÈç¹ûÒ»¸öÊýÔÚabÖÐ×Ü¹²³öÏÖ³¬¹ýn´Î¾Í¿Ï¶¨ÎÞ½â
ÓÉÓÚÒ»¿ªÊ¼¶¼ÊÇÉýÐò£¬°Ñ³õÊ¼µÄbÊ×Î²µ÷»»Ò»ÏÂ¿ÉÄÜ»á¸ü¿ì
*/
int n;
int a[200005], b[200005], cnt[200005];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  for (int i = n; i >= 1; i--) {
    cin >> b[i];
    cnt[b[i]]++;
    if (cnt[b[i]] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  int top = 1, end = n;
  for (int i = 1; i <= n; i++) {
    while (a[i] == b[i]) {
      if (b[top] != a[i] && a[top] != b[i])
        swap(b[top], b[i]);
      else if (b[end] != a[i] && a[top] != b[i])
        swap(b[i], b[end]);
      else {
        top++;
        end--;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    cout << b[i] << ' ';
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n, a[1000000], b[1000000];
int f = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int j = n; j >= 1; j--)
    cin >> b[j];
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) {
      f++;
      // int l=lower_bound(b+i+1,b+1+n,a[i])-b;
      for (int j = i + 1; j <= n; j++) {
        if (a[i] != b[j]) {
          swap(b[i], b[j]);
          f--;
          break;
        }
      }
    }
  }

  if (f)
    cout << "No";
  else {
    puts("Yes");
    for (int i = 1; i <= n; i++) {
      cout << b[i];
      if (i < n)
        cout << " ";
    }
  }
  // system("pause");
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

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

int n, a[200000], b[200000];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  reverse(b, b + n);
  int l = -1, r = -1;
  for (int i = 0; i < n; i++)
    if (a[i] == b[i]) {
      int nl = i, nr = i - 1;
      for (int j = i; a[j] == b[j] && j < n; j++)
        nr++;
      l = nl, r = nr;
      if (r - l + 1 == n) {
        cout << "No\\n";
        return 0;
      }
      break;
    }
  if (l != -1) {
    int j = l;
    for (int i = 0; i < l && j <= r; i++)
      if (a[i] != b[j] && a[j] != b[i])
        swap(b[i], b[j++]);
    for (int i = r + 1; i < n && j <= r; i++)
      if (a[i] != b[j] && a[j] != b[i])
        swap(b[i], b[j++]);
    if (j <= r) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (int i = 0; i < n; i++)
    cout << b[i] << " \\n"[i == n - 1];
  return 0;
}

Error: WA

Fixed submission:

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

int n, a[200000], b[200000];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  reverse(b, b + n);
  int l = -1, r = -1;
  for (int i = 0; i < n; i++)
    if (a[i] == b[i]) {
      int nl = i, nr = i - 1;
      for (int j = i; a[j] == b[j]; j++)
        nr++;
      l = nl, r = nr;
      break;
    }
  if (l != -1) {
    int j = l;
    for (int i = 0; i < l && j <= r; i++)
      if (a[i] != b[j] && a[j] != b[i])
        swap(b[i], b[j++]);
    for (int i = r + 1; i < n && j <= r; i++)
      if (a[i] != b[j] && a[j] != b[i])
        swap(b[i], b[j++]);
    if (j <= r) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (int i = 0; i < n; i++)
    cout << b[i] << " \\n"[i == n - 1];
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  uint64_t n;
  cin >> n;
  vector<int> a(n + 1, 0);
  vector<int> b(n + 1, 0);
  vector<int> A(n);
  vector<int> B(n);
  int curr_a = 1;
  int curr_b = 1;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    for (int j = curr_a; j < A[i]; j++) {
      a[j] = i;
    }
    curr_a = A[i];
  }
  for (int i = curr_a; i <= n; i++) {
    a[i] = n;
  }
  for (int i = 0; i < n; i++) {
    cin >> B[i];
    for (int j = curr_b; j < B[i]; j++) {
      b[j] = i;
    }
    curr_b = B[i];
  }
  for (int i = curr_b; i <= n; i++) {
    b[i] = n;
  }
  int x = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] - a[i - 1] + b[i] - b[i - 1] > n) {
      cout << "No" << endl;
      return 0;
    } else if (a[i] - b[i - 1] > x) {
      x = a[i] - b[i - 1];
    }
  }
  cout << "Yes" << endl;
  for (int i = n - x; i < n; i++) {
    cout << B[i] << " ";
  }
  for (int i = 0; i < n - x; i++) {
    if (i < n - x - 1) {
      cout << B[i] << " ";
    } else {
      cout << B[i];
    }
  }
  cout << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  uint32_t n;
  cin >> n;
  vector<uint32_t> a(n + 1, 0);
  vector<uint32_t> b(n + 1, 0);
  vector<uint32_t> A(n);
  vector<uint32_t> B(n);
  uint32_t curr_a = 1;
  uint32_t curr_b = 1;
  for (uint32_t i = 0; i < n; i++) {
    cin >> A[i];
    for (uint32_t j = curr_a; j < A[i]; j++) {
      a[j] = i;
    }
    curr_a = A[i];
  }
  for (uint32_t i = curr_a; i <= n; i++) {
    a[i] = n;
  }
  for (uint32_t i = 0; i < n; i++) {
    cin >> B[i];
    for (uint32_t j = curr_b; j < B[i]; j++) {
      b[j] = i;
    }
    curr_b = B[i];
  }
  for (uint32_t i = curr_b; i <= n; i++) {
    b[i] = n;
  }
  uint32_t x = 0;
  for (uint64_t i = 1; i <= n; i++) {
    if (a[i] - a[i - 1] + b[i] - b[i - 1] > n) {
      cout << "No" << endl;
      return 0;
    } else if (a[i] - b[i - 1] > x) {
      x = a[i] - b[i - 1];
    }
  }
  cout << "Yes" << endl;
  for (uint32_t i = x; i < n; i++) {
    if (x > 0 || i < n - 1) {
      cout << B[i] << " ";
    } else {
      cout << B[i];
    }
  }
  for (uint32_t i = 0; i < x; i++) {
    if (i < x - 1) {
      cout << B[i] << " ";
    } else {
      cout << B[i];
    }
  }
  cout << endl;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

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

int N, A[200000], B[200000];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }
  reverse(B, B + N);
  int f = 0;
  int b = N - 1;
  for (int i = 0; i < N; i++) {
    if (A[i] == B[i]) {
      if (B[f] != A[i] && A[f] != B[i]) {
        swap(B[f], B[i]);
        f++;
      } else {
        swap(B[b], B[i]);
        b--;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (A[i] == B[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  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>
using namespace std;

int N, A[200000], B[200000], C[200000], D[200000];

int main() {
  cin >> 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]--;
  }
  for (int i = 0; i < N; i++) {
    C[A[i]]++;
    D[B[i]]++;
  }
  for (int i = 0; i < N; i++) {
    if (C[i] + D[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  reverse(B, B + N);
  int f = 0;
  int b = N - 1;
  for (int i = 0; i < N; i++) {
    if (A[i] == B[i]) {
      if (B[f] != A[i]) {
        swap(B[f], B[i]);
        f++;
      } else {
        swap(B[b], B[i]);
        b--;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    assert(A[i] != B[i]);
  }
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << B[i] + 1;
    if (i != N - 1)
      cout << ' ';
  }
  cout << endl;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mkp make_pair
#define fi first
#define se second
void outputno() {
  puts("No");
  exit(0);
}
int n, m;
map<int, int> mp, mp1;
multiset<piii> st;
int main() {
  n = 2, cin >> m;
  vector<vector<int>> a(n + 1), ans(n + 1);
  for (int i = 1; i <= n; i++)
    a[i].resize(m + 1), ans[i].resize(m + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", &a[i][j]);
  for (int i = 1; i <= m; i++)
    ans[1][i] = a[1][i];
  for (int i = 2; i <= n; i++) {
    mp.clear(), mp1.clear(), st.clear();
    for (int j = 1; j <= m; j++)
      mp1[a[i][j]]++, mp[a[i - 1][j]]++, mp[a[i][j]]++;
    for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
      if (mp1[it->first] > 0)
        st.insert(mkp(-it->second, mkp(mp1[it->first], it->first)));
    }
    for (int j = 1; j <= m; j++) {
      piii s = (*st.begin()), ss;
      st.erase(st.begin());
      ss = s;
      if (-s.fi > m - j + 1)
        outputno();
      if (s.se.se == ans[i - 1][j])
        s = (*st.begin()), st.erase(st.begin()), st.insert(ss);
      ans[i][j] = s.se.se;
      int tmp = --mp1[s.se.se], tmp2 = --mp[s.se.se];
      if (tmp > 0)
        st.insert(mkp(-tmp2, mkp(tmp, s.se.se)));
      tmp = mp1[ans[i - 1][j]];
      if (tmp > 0) {
        int val = mp[ans[i - 1][j]]--;
        st.erase(st.find(mkp(-val, mkp(tmp, ans[i - 1][j]))));
        val--;
        st.insert(mkp(-val, mkp(tmp, ans[i - 1][j])));
      }
    }
  }
  puts("Yes");
  for (int j = 1; j <= m; j++)
    printf("%d ", ans[2][j]);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define mkp make_pair
#define fi first
#define se second
int n, a[200004], ans[200004];
set<pii> pq;
map<int, int> mp, mp1;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> ans[i];
    mp[ans[i]]++, mp[a[i]]++;
    mp1[ans[i]]++;
  }
  for (map<int, int>::iterator it = mp1.begin(); it != mp1.end(); it++)
    pq.insert(mkp(-mp[it->first], it->first));
  for (int i = 0; i < n; i++) {
    if (pq.empty())
      return 0;
    pii O = (*pq.begin());
    pq.erase(pq.begin());
    if (O.se != a[i]) {
      ans[i] = O.se;
      O.fi++;
    } else {
      if (pq.empty()) {
        puts("No");
        return 0;
      }
      O = (*pq.begin());
      pq.erase(pq.begin());
      pq.insert(mkp(-mp[a[i]], a[i]));
      ans[i] = O.se;
      O.fi++;
    }
    mp1[O.se]--;
    if (mp1[O.se] > 0)
      pq.insert(O);
    if (mp1.find(a[i]) != mp1.end() && mp1[a[i]] > 0) {
      pq.erase(pq.find(mkp(-mp[a[i]], a[i])));
      mp[a[i]]--;
      pq.insert(mkp(-mp[a[i]], a[i]));
    }
  }
  puts("Yes");
  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n;
inline int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return f * x;
}
const int MAXN = 201000;
int a[MAXN], b[MAXN], c[MAXN], l, r, x;
int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i <= n; ++i)
    c[i] = read();
  for (int i = n; i >= 1; --i)
    b[i] = c[n - i + 1];
  x = -1;
  bool flag = false;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i] && x == -1) {
      l = i;
      x = a[i];
      r = i;
      flag = true;
    } else if (x != -1 && a[i] == b[i])
      r = i;
  }
  if (flag) {
    for (int i = 1; i <= n; ++i) {
      if (x != a[i] && x != b[i] && l <= r) {
        l++;
        swap(b[i], b[l - 1]);
      }
    }
  }
  if (l <= r && flag)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int i = 1; i <= n; ++i)
      cout << b[i] << " ";
  }
  system("pause");
  return 0;
}


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
ll n, m, ans;
ll a[200005], b[200005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  ll l = 1, r = n;
  while (l <= r) {
    ll is = 0;
    while (a[l] != b[l] && l <= r)
      l++, is = 1;
    while (a[r] != b[r] && l <= r)
      r--, is = 1;
    while (a[l] == b[l] && a[r] == b[r] && b[l] != b[r] && l < r) {
      swap(b[l], b[r]);
      l++, r--;
      is = 1;
    }
    if (!is)
      break;
  }
  while (l <= r) {
    ll is = 0;
    for (int i = 1; i <= n; i++) {
      if (a[l] != a[i] && b[l] != b[i] && l <= r) {
        swap(b[l], b[i]);
        l++;
        is = 1;
        break;
      }
    }
    if (!is) {
      cout << "No";
      // system("pause");
      return 0;
    }
  }
  cout << "Yes\\n";
  for (int i = 1; i <= n; i++)
    cout << b[i] << " ";
  // system("pause");
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PI 3.141592653589793238462
using namespace std;
typedef long long ll;
typedef long double db;
ll a[200005], b[200005], p[200005];
int main() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    p[a[i]]++;
  }
  for (ll i = 1; i <= n; i++) {
    cin >> b[i];
    p[b[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  reverse(b + 1, b + 1 + n);
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) {
      for (int j = 1; j <= n; j++) {
        if (b[j] != a[i] && b[i] != a[j]) {
          swap(b[i], b[j]);
          break;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << b[i] << " ";
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PI 3.141592653589793238462
using namespace std;
typedef long long ll;
typedef long double db;
ll a[200005], b[200005], p[200005];
int main() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    p[a[i]]++;
  }
  for (ll i = 1; i <= n; i++) {
    cin >> b[i];
    p[b[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) {
      for (int j = 1; j <= n; j++) {
        if (b[j] != a[i] && b[i] != a[j]) {
          swap(b[i], b[j]);
          break;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << b[i] << " ";
  }
  cout << endl;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#pragma GCC optimization("O3")

#define endl '\\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define ld long double
#define bit(idx) idx & (-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define de(x) cout << #x << " = " << x << endl;
#define row vector<ll>
#define matrix vector<row>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
/// find_by_order(x) -> x-th element in the set
/// order_of_key(x)  -> how many elements are smaller than x
/// insert(x) -> inserts x into the set

int main() {
  /// ios_base::sync_with_stdio(false); cin.tie(NULL);
  ll n;
  cin >> n;
  vector<ll> A(n), B(n), cnt(n + 2);
  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++) {
    ++cnt[A[i]];
    ++cnt[B[i]];
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[i] > n) {
      cout << "No\\n";
      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\\n";
  for (auto x : B)
    cout << x << " ";
  cout << endl;
}
/**

*/

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#pragma GCC optimization("O3")

#define endl '\\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define ld long double
#define bit(idx) idx & (-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define de(x) cout << #x << " = " << x << endl;
#define row vector<ll>
#define matrix vector<row>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
/// find_by_order(x) -> x-th element in the set
/// order_of_key(x)  -> how many elements are smaller than x
/// insert(x) -> inserts x into the set

int main() {
  /// ios_base::sync_with_stdio(false); cin.tie(NULL);
  ll n;
  cin >> n;
  vector<ll> A(n), B(n), cnt(n + 2);
  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++) {
    ++cnt[A[i]];
    ++cnt[B[i]];
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[i] > n) {
      cout << "No\\n";
      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\\n";
  for (auto x : B)
    cout << x << " ";
  cout << endl;
}
/**

*/