Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

// #pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 9;
const int infint = 1e9 + 7;
const int MAXN = 2e5 + 7;
const double eps = 1e-9;
const long double pi = 3.1415926535897932384626433832795;
const long double e = 2.71828182845904523536028747135266;
inline ll read() {
  ll kr = 1, xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls)) {
    if (!(ls ^ 45))
      kr = -1;
    ls = getchar();
  }
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs * kr;
}
// inline __int128 read128()
// {
//     __int128 kr=1,xs=0;char ls;ls=getchar();
//     while(!isdigit(ls)){
//         if(!(ls^45)) kr=-1;ls=getchar();
//     }
//     while(isdigit(ls)){
//         xs=(xs<<1)+(xs<<3)+(ls^48);ls=getchar();
//     }
//     return xs*kr;
// }
inline ull readull() {
  ull xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls))
    ls = getchar();
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs;
}
inline void writeull(ull x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10)
    writeull(x / 10);
  putchar('0' + x % 10);
}
inline void write(ll x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar('0' + x % 10);
}
// inline void write(__int128 x){
//     if(x<0){x*=-1;putchar('-');}
//     if(x>=10) write(x/10);
//     putchar('0'+x%10);
// }
///////////////////////////////////////////////////////////////////////////////////////
int a[MAXN], b[MAXN], c[MAXN], d[MAXN], n;
int main() {
  // ios_base::sync_with_stdio(0);
  // cin.tie(0),cout.tie(0);
  int t = 1;
  // t = read();
  while (t--) {
    n = read();
    int flag = 0;
    for (int i = 0; i < n; i++) {
      a[i] = read();
      c[a[i]]++;
    }
    for (int i = 0; i < n; i++) {
      b[i] = read();
      d[b[i]]++;
    }
    for (int i = 1; i <= n; i++) {
      if (c[i] + d[i] > n) {
        cout << "No" << endl;
        return 0;
      }
      c[i] += c[i - 1];
      d[i] += d[i - 1];
    }
    for (int i = 1; i <= n; i++) {
      flag = max(flag, c[i] - d[i - 1]);
    }
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++) {
      if (i)
        cout << " ";
      cout << b[(i - flag + n) % n];
    }
    cout << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// #pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 9;
const int infint = 1e9 + 7;
const int MAXN = 2e5 + 7;
const double eps = 1e-9;
const long double pi = 3.1415926535897932384626433832795;
const long double e = 2.71828182845904523536028747135266;
inline ll read() {
  ll kr = 1, xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls)) {
    if (!(ls ^ 45))
      kr = -1;
    ls = getchar();
  }
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs * kr;
}
// inline __int128 read128()
// {
//     __int128 kr=1,xs=0;char ls;ls=getchar();
//     while(!isdigit(ls)){
//         if(!(ls^45)) kr=-1;ls=getchar();
//     }
//     while(isdigit(ls)){
//         xs=(xs<<1)+(xs<<3)+(ls^48);ls=getchar();
//     }
//     return xs*kr;
// }
inline ull readull() {
  ull xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls))
    ls = getchar();
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs;
}
inline void writeull(ull x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10)
    writeull(x / 10);
  putchar('0' + x % 10);
}
inline void write(ll x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar('0' + x % 10);
}
// inline void write(__int128 x){
//     if(x<0){x*=-1;putchar('-');}
//     if(x>=10) write(x/10);
//     putchar('0'+x%10);
// }
///////////////////////////////////////////////////////////////////////////////////////
int a[MAXN], b[MAXN], c[MAXN], n;
int check() {
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i])
      return 1;
  }
  return 0;
}
int main() {
  // ios_base::sync_with_stdio(0);
  // cin.tie(0),cout.tie(0);
  int t = 1;
  // t = read();
  while (t--) {
    n = read();
    int flag = 0;
    for (int i = 1; i <= n; i++) {
      a[i] = read();
      c[a[i]]++;
    }
    for (int i = 1; i <= n; i++) {
      b[i] = read();
      c[b[i]]++;
      if (c[b[i]] > n) {
        cout << "No" << endl;
        flag = 1;
        break;
      }
    }
    if (flag)
      continue;
    cout << "Yes" << endl;
    while (check())
      random_shuffle(b + 1, b + 1 + n);
    for (int i = 1; i <= n; i++) {
      if (i != 1)
        cout << " ";
      cout << b[i];
    }
    cout << endl;
  }
  return 0;
}