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

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N;
  int A[200200];
  int B[200200];
  int ACount[200200];
  int BCount[200200];
  for (int i = 0; i < 200200; i++) {
    ACount[i] = 0;
    BCount[i] = 0;
  }
  clock_t start = clock();
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    ACount[A[i]]++;
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
    BCount[B[i]]++;
  }
  int over = 0;
  for (int i = 1; i <= N; i++) {
    if (ACount[i] > 0 && BCount[i] > 0) {
      over = max(over, ACount[i] + BCount[i]);
    }
  }
  if (over > N) {
    cout << "No" << endl;
    return 0;
  } else {
    cout << "Yes" << endl;
  }
  reverse(B, B + N);
  int counter = 0;
  std::random_device rnd;
  while (static_cast<double>(clock() - start) / CLOCKS_PER_SEC * 1000.0 <
         1920) {
    for (int i = 0; i < N; i++) {
      if (A[i] == B[i]) {
        int next = rnd() % N;
        int temp = B[next];
        B[next] = B[i];
        B[i] = temp;
        if (A[next] == B[next]) {
          int nextnext = rand() % N;
          int temp2 = B[nextnext];
          B[nextnext] = B[next];
          B[next] = temp2;
        }
      }
    }
  }

  for (int i = 0; i < N; i++) {
    cout << B[i] << " ";
  }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N;
  int A[200200];
  int B[200200];
  int ACount[200200];
  int BCount[200200];
  for (int i = 0; i < 200200; i++) {
    ACount[i] = 0;
    BCount[i] = 0;
  }
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    ACount[A[i]]++;
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
    BCount[B[i]]++;
  }
  int over = 0;
  for (int i = 1; i <= N; i++) {
    if (ACount[i] > 0 && BCount[i] > 0) {
      over = max(over, ACount[i] + BCount[i]);
    }
  }
  if (over > N) {
    cout << "No" << endl;
    return 0;
  } else {
    cout << "Yes" << endl;
  }

  int counter = 0;
  for (int t = 0; t < 500000000 / N; t++) {
    for (int i = 0; i < N; i++) {
      int next = (i + counter) % N;
      if (A[i] == B[i]) {
        int temp = B[next];
        B[next] = B[i];
        B[i] = temp;
      }
      counter += 3;
      counter *= counter;
      counter %= N;
    }
  }

  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>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

int main() {
  int N;
  cin >> N;

  vec<int> A(N);
  map<int, int> mpA;
  rep(i, N) {
    cin >> A[i];
    ++mpA[A[i]];
  }

  vec<int> B(N);
  map<int, int> mpB;
  rep(i, N) {
    cin >> B[i];
    ++mpB[B[i]];
  }

  for (int i = 1; i <= N; ++i) {
    if (mpA[i] + mpB[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << '\\n';

  int now = 0;
  rep(i, N) {
    if (A[i] == B[i]) {
      for (int j = now; j < N; j = (j + 1) % N) {
        if (A[j] != B[i] && B[i] != B[j]) {
          swap(B[i], B[j]);
          now = j;
          break;
        }
      }
    }
  }
  rep(i, N) cout << B[i] << " ";
  cout << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

int main() {
  int N;
  cin >> N;

  vec<int> A(N);
  map<int, int> mpA;
  rep(i, N) {
    cin >> A[i];
    ++mpA[A[i]];
  }

  vec<int> B(N);
  map<int, int> mpB;
  rep(i, N) {
    cin >> B[i];
    ++mpB[B[i]];
  }

  for (int i = 1; i <= N; ++i) {
    if (mpA[i] + mpB[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << '\\n';
  sort(all(B), greater<int>());
  rep(i, N) {
    if (A[i] == B[i]) {
      int k = upper_bound(all(A), B[i]) - A.begin();
      while (k < N && B[i] == B[k])
        ++k;
      if (k == N) {
        if (B[0] != B[i] && A[0] != B[i]) {
          swap(B[i], B[0]);
        } else {
          cout << "No" << endl;
          return 0;
        }
      } else {
        swap(B[i], B[k]);
      }
    }
  }
  rep(i, N) cout << B[i] << " ";
  cout << '\\n';
}
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(0);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  vector<int> acount(n + 1, 0);
  vector<int> bcount(n + 1, 0);
  for (int i = 0; i < n; i++) {
    acount[a[i]]++;
    bcount[b[i]]++;
  }
  bool ok = true;
  for (int i = 1; i <= n; i++) {
    if (acount[i] + bcount[i] > n) {
      ok = false;
    }
  }
  if (!ok)
    cout << "No" << endl;
  else {
    vector<int> asum(n + 1, 0);
    vector<int> bsum(n + 1, 0);
    for (int i = 1; i <= n; i++) {
      asum[i] = asum[i - 1] + acount[i];
      bsum[i] = bsum[i - 1] + bcount[i];
    }
    int rot = 0;
    for (int i = 1; i <= n; i++) {
      rot = max(rot, asum[i] - bsum[i - 1]);
    }
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++) {
      if (i != 0)
        cout << ' ';
      cout << b[(i - rot + n) % n];
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  int ans = -1;
  for (int i = 0; i < n; i++) {
    bool ok = true;
    for (int j = 0; j < n; j++) {
      if (a[j] == b[(j + i) % n]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ans = i;
      break;
    }
  }
  if (ans == -1)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++) {
      if (i != 0)
        cout << ' ';
      cout << b[(ans + i) % n];
    }
  }
  cout << endl;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const long long mod = 1000000007;
int main() {
  long long i, j, n, x, h = 0, s;
  cin >> n;
  vector<long long> a, b, c;
  for (i = 1; i <= n; i++) {
    cin >> x;
    a.push_back(x);
  }
  for (i = 1; i <= n; i++) {
    cin >> x;
    b.push_back(x);
  }
  s = (n >> 1);
  for (i = s; i < n; i++) {
    c.push_back(b[i]);
  }
  for (i = 0; i < s; i++) {
    c.push_back(b[i]);
  }
  h = 0;
  for (j = 0; j < n; j++) {
    if (a[j] == c[j]) {
      h++;
      break;
    }
  }
  if (h == 0) {
    cout << "Yes" << endl;
    for (j = 0; j < n; j++) {
      cout << c[j] << " ";
    }
    cout << endl;
  } else {
    reverse(b.begin(), b.end());
    h = 0;
    for (j = 0; j < n; j++) {
      if (a[j] == b[j]) {
        h++;
        break;
      }
    }
    if (h == 0) {
      cout << "Yes" << endl;
      for (j = 0; j < n; j++) {
        cout << b[j] << " ";
      }
      cout << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const long long mod = 1000000007;
int main() {
  long long i, j, n, x, h = 0;
  cin >> n;
  vector<long long> a, b, c;
  for (i = 1; i <= n; i++) {
    cin >> x;
    a.push_back(x);
  }
  for (i = 1; i <= n; i++) {
    cin >> x;
    b.push_back(x);
    c.push_back(x);
  }
  sort(b.begin(), b.end(), greater<long long>());
  for (i = 1; i >= 1; i++) {
    h = 0;
    for (j = 0; j < n; j++) {
      if (a[j] == b[j]) {
        h++;
      }
    }
    if (h == 0) {
      cout << "Yes" << endl;
      for (j = 0; j < n; j++) {
        cout << b[j] << " ";
      }
      cout << endl;
      return 0;
    } else {
      if (b == c) {
        cout << "No" << endl;
        return 0;
      }
      prev_permutation(b.begin(), b.end());
    }
  }
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define SF scanf
#define PF printf
#define MAXN 200010
#define MOD 1000000007
using namespace std;
typedef long long ll;
int n;
int a[MAXN], b[MAXN], st, ed, cnt1, cnt2;
int main() {
  SF("%d", &n);
  for (int i = 1; i <= n; i++)
    SF("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    SF("%d", &b[i]);
  reverse(b + 1, b + 1 + n);
  for (int i = 1; i <= n; i++)
    if (a[i] == b[i]) {
      st = i;
      break;
    }
  for (int i = n; i >= 1; i--)
    if (a[i] == b[i]) {
      ed = i;
      break;
    }
  if (st == 0) {
    PF("Yes\\n");
    for (int i = 1; i <= n; i++)
      PF("%d ", b[i]);
    return 0;
  }
  int maxv = a[st];
  for (int i = 1; i <= n; i++) {
    if (a[i] == maxv)
      cnt1++;
    if (b[i] == maxv)
      cnt2++;
  }
  int j = 1;
  for (int i = st; i <= ed; i++) {
    for (; j <= n; j++)
      if (b[i] != a[j] && a[i] != b[j]) {
        swap(b[i], b[j]);
        j++;
        break;
      }
  }
  for (int i = 1; i <= n; i++)
    if (a[i] == b[i]) {
      PF("No");
      return 0;
    }
  PF("Yes\\n");
  for (int i = 1; i <= n; i++)
    PF("%d ", b[i]);
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define SF scanf
#define PF printf
#define MAXN 2010
#define MOD 1000000007
using namespace std;
typedef long long ll;
int n;
int a[MAXN], b[MAXN], st, ed, cnt1, cnt2;
int main() {
  SF("%d", &n);
  for (int i = 1; i <= n; i++)
    SF("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    SF("%d", &b[i]);
  reverse(b + 1, b + 1 + n);
  for (int i = 1; i <= n; i++)
    if (a[i] == b[i]) {
      st = i;
      break;
    }
  for (int i = n; i >= 1; i--)
    if (a[i] == b[i]) {
      ed = i;
      break;
    }
  if (st == 0) {
    PF("Yes\\n");
    for (int i = 1; i <= n; i++)
      PF("%d ", b[i]);
    return 0;
  }
  int maxv = a[st];
  for (int i = 1; i <= n; i++) {
    if (a[i] == maxv)
      cnt1++;
    if (b[i] == maxv)
      cnt2++;
  }
  int j = 1;
  for (int i = st; i <= ed; i++) {
    for (; j <= n; j++)
      if (b[i] != a[j] && a[i] != b[j]) {
        swap(b[i], b[j]);
        break;
      }
  }
  for (int i = 1; i <= n; i++)
    if (a[i] == b[i]) {
      PF("No");
      return 0;
    }
  PF("Yes\\n");
  for (int i = 1; i <= n; i++)
    PF("%d ", b[i]);
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define mem(a, x) memset(a, x, sizeof(a));
#define ft                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int main() {
  ft int t, i, j, n, m, l, r;
  cin >> n;
  int a[n + 1], b[n + 1], c[n + 1];
  mem(c, 0);
  for (i = 0; i < n; i++) {
    cin >> a[i];
    c[a[i]]++;
  }
  for (i = 0; i < n; i++) {
    cin >> b[i];
    c[b[i]]++;
  }
  for (i = 1; i <= n; i++) {
    if (c[i] > n) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  reverse(b, b + n);
  for (i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      for (j = 0; j < n; j++) {
        if (b[j] != a[i] && b[i] != a[j]) {
          swap(b[i], b[j]);
          break;
        }
      }
    }
  }
  for (i = 0; i < n; i++) {
    cout << b[i] << " ";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mem(a, x) memset(a, x, sizeof(a));
#define ft                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int main() {
  ft int t, i, j, n, m, l, r;
  cin >> n;
  int a[n + 1], b[n + 1], c[n + 1];
  mem(c, 0);
  for (i = 0; i < n; i++) {
    cin >> a[i];
    c[a[i]]++;
  }
  for (i = 0; i < n; i++) {
    cin >> b[i];
    c[b[i]]++;
  }
  for (i = 1; i <= n; i++) {
    if (c[i] > n) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      for (j = 0; j < n; j++) {
        if (b[j] != a[i] && b[i] != a[j]) {
          swap(b[i], b[j]);
          break;
        }
      }
    }
  }
  for (i = 0; i < n; i++) {
    cout << b[i] << " ";
  }
  return 0;
}
Problem p02557 (C++) — buggy submission, verdict: Runtime Error

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

Error: WA

Fixed submission:

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

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

int n;
vector<int> a, b, res;

bool solve();

int main() {
  cin >> n;
  a.resize(n);
  for (auto &p : a)
    cin >> p;
  b.resize(n);
  for (auto &p : b)
    cin >> p;
  if (solve()) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; ++i)
      cout << res[i] << " \\n"[i == n - 1];
  } else
    cout << "No" << endl;
  return 0;
}

bool solve() {
  vector<int> cnta(n + 1, 0), cntb(n + 1, 0);
  for (int i = 0; i < n; ++i) {
    ++cnta[a[i]];
    ++cntb[b[i]];
  }
  for (int i = 1; i <= n; ++i) {
    if (cnta[i] + cntb[i] > n)
      return 0;
    cnta[i] += cnta[i - 1];
    cntb[i] += cntb[i - 1];
  }
  int dif = 0;
  for (int i = 1; i <= n; ++i)
    dif = max(dif, cnta[i] - cntb[i - 1]);
  res.resize(n);
  for (int i = 0; i < n; ++i)
    res[(i + dif) % n] = b[i];
  return 1;
}

Error: TLE

Fixed submission:

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

int n;
vector<int> a, b, res;

bool solve();

int main() {
  cin >> n;
  a.resize(n);
  for (auto &p : a)
    cin >> p;
  b.resize(n);
  for (auto &p : b)
    cin >> p;
  if (solve()) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; ++i)
      cout << res[i] << " \\n"[i == n - 1];
  } else
    cout << "No" << endl;
  return 0;
}

bool solve() {
  {
    vector<int> cnt(n + 1, 0);
    for (int i = 0; i < n; ++i) {
      ++cnt[a[i]];
      ++cnt[b[i]];
    }
    for (auto p : cnt)
      if (p > n)
        return 0;
  }
  vector<int> cnta(n + 1, 0);
  for (auto p : a)
    ++cnta[p];
  for (int x = 1000000; x < 1000000 + n; ++x) {
    bool ch = 1;
    set<int> st;
    res.resize(n);
    for (int i = 0; i < 2 * n; ++i)
      st.insert(i);
    for (int i = 0; i < n; ++i)
      if (cnta[b[(i + x) % n]]) {
        int id = upper_bound(a.begin(), a.end(), b[(i + x) % n]) - a.begin();
        int now = *(st.lower_bound(id));
        now %= n;
        res[now] = b[(i + x) % n];
        if (res[now] == a[now]) {
          ch = 0;
          break;
        }
        st.erase(now);
        st.erase(now + n);
      }
    for (int i = 0; i < n; ++i)
      if (!cnta[b[(i + x) % n]]) {
        int id = upper_bound(a.begin(), a.end(), b[(i + x) % n]) - a.begin();
        int now = *(st.lower_bound(id));
        now %= n;
        res[now] = b[(i + x) % n];
        if (res[now] == a[now]) {
          ch = 0;
          break;
        }
        st.erase(now);
        st.erase(now + n);
      }
    if (ch)
      break;
  }
  return 1;
}
Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = a; i < (b); ++i)
#define vl vector<ll>

int main() {

  ll N;
  cin >> N;

  vl A(N);
  vl B(N);
  vl a(N + 1, 0);
  vl b(N + 1, 0);

  rep(i, N) {
    cin >> A[i];
    a[A[i]]++;
  }

  rep(i, N) {
    cin >> B[i];
    b[B[i]]++;
  }

  FOR(i, 1, N + 1) {
    if (a[i] + b[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  FOR(i, 1, N + 1) {
    a[i] += a[i - 1];
    b[i] += b[i - 1];
  }

  ll k = 0;
  FOR(i, 1, N + 1) { k = max(k, a[i] - b[i - 1]); }

  rep(i, N) { cout << B[(i + N - k) % N] << " "; }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

bool check(vl A, vl B, ll k) {
  bool ok = true;
  ll n = A.size();
  rep(i, n) {
    if (A[i] == B[(i + k) % n]) {
      ok = false;
      break;
    }
  }
  return ok;
}

int main() {

  ll N;
  cin >> N;

  vl A(N);
  vl B(N);
  vl a(N + 1, 0);
  vl b(N + 1, 0);

  rep(i, N) {
    cin >> A[i];
    a[A[i]]++;
  }

  rep(i, N) {
    cin >> B[i];
    b[B[i]]++;
  }

  vl c(N + 1);
  ll amax = 0;
  ll bmax = 0;

  ll amax_i = 0;
  ll bmax_i = 0;

  rep(i, N + 1) {
    if (a[i] > amax) {
      amax = a[i];
      amax_i = i;
    }
    if (b[i] > bmax) {
      bmax = b[i];
      bmax_i = i;
    }
    c[i] = a[i] + b[i];
    if (c[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;

  ll amax_end = 0;
  rep(i, amax_i + 1) { amax_end += a[i]; }
  ll amax_begin = 0;
  rep(i, amax_i) { amax_begin += b[i]; }

  ll k;
  k = (N + amax_end - amax_begin) % N;
  if (check(A, B, k)) {
    rep(i, B.size()) { cout << B[(i + k) % N] << " "; }
    cout << endl;
    return 0;
  }
  while (true) {
    k = rand() % N;
    if (check(A, B, k)) {
      rep(i, B.size()) { cout << B[(i + k) % N] << " "; }
      cout << endl;
      break;
    }
  }

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

#include <bits/stdc++.h>

#define ll long long
#define rep(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>

#define fi first
#define se second
#define all(A) A.begin(), A.end()

using namespace std;

/////////////////////////////////////////////////////
#define MOD (ll)(1e9 + 7)
#define vv vector

ll N;
ll A[202020], B[202020];
ll i, j, k;
ll mk[202020], mka[202020], mkb[202020];

ll anss[202020];

int main() {
  scanf("%lld", &N);

  rep(i, 0, N) {
    scanf("%lld", &A[i]);
    ++mka[A[i]];
  }
  rep(i, 0, N) {
    scanf("%lld", &B[i]);
    ++mkb[B[i]];
  }
  rep(i, 0, N + 1) {
    if (N < mka[i] + mkb[i]) {
      break;
    }
  }

  if (i < N + 1) {
    printf("No\\n");
    return 0;
  }

  ll a = 0;
  rep(i, 0, N) {

    while (i + a < N && A[i + a] <= B[i]) {
      ++a;
    }
  }
  rep(i, 0, N) { anss[(i + a) % N] = B[i]; }

  printf("Yes\\n");
  rep(i, 0, N) { printf("%lld ", anss[i]); }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define rep(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>

#define fi first
#define se second
#define all(A) A.begin(), A.end()

using namespace std;

/////////////////////////////////////////////////////
#define MOD (ll)(1e9 + 7)
#define vv vector

ll N;
ll A[202020], B[202020];
ll i, j, k;
ll mk[202020], mka[202020], mkb[202020];

ll anss[202020];

int main() {
  scanf("%lld", &N);

  rep(i, 0, N) {
    scanf("%lld", &A[i]);
    ++mka[A[i]];
  }
  rep(i, 0, N) {
    scanf("%lld", &B[i]);
    ++mkb[B[i]];
  }

  rep(i, 0, N + 1) {
    if (N < mka[i] + mkb[i]) {
      break;
    }
  }

  if (i < N + 1) {
    printf("No\\n");
    return 0;
  }
  ll a = 0;
  rep(i, 1, N) {
    if (mka[A[i]] > mka[A[0]]) {
      a = i;
    }
  }
  j = a;
  list<ll> blist;

  rep(i, 0, N) { blist.push_back((i + a) % N); }

  auto bbit = blist.begin();

  rep(i, 0, N) {
    ll ii = (i + a) % N;
    while (A[i] == B[*bbit]) {
      ++bbit;
      if (bbit == blist.end()) {
        bbit = blist.begin();
      }
    }
    anss[i] = B[*bbit];
    bbit = blist.erase(bbit);
    if (bbit == blist.end())
      bbit = blist.begin();
  }

  printf("Yes\\n");
  rep(i, 0, N) { printf("%lld ", anss[i]); }
  return 0;
}