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

#include <bits/stdc++.h>
#define FLASH ios_base::sync_with_stdio(0);
#define ll long long
#define debt(x, y)                                                             \\
  cout << "#x = " << (x) << " and "                                            \\
       << "#y = " << (y) << endl;
#define deb(x) cout << "#x = " << (x) << endl;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define arr(a, n)                                                              \\
  for (ll i = 1; i <= n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";
#define vecc(a, n)                                                             \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";

using namespace std;

const ll RANDOM =
    chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);

ll n;
vector<ll> a;
vector<ll> b;

ll T = 100;

int main() {
  FLASH;
  srand(time(0));
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
  }

  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    b.pb(x);
  }

  vector<ll> cp = b;
  ll sig = 0;

  for (ll i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      sig = 1;
      break;
    }
  }

  if (sig == 0) {
    cout << "Yes" << endl;
    vecc(b, n);
    return 0;
  }

  reverse(all(b));
  sig = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      sig = 1;
      break;
    }
  }

  if (sig == 0) {
    cout << "Yes" << endl;
    vecc(b, n);
    return 0;
  }

  b = cp;
  while (T--) {
    shuffle(all(b), rng);
    sig = 0;
    for (ll i = 0; i < n; i++) {
      if (a[i] == b[i]) {
        sig = 1;
        break;
      }
    }
    if (sig == 0) {
      cout << "Yes" << endl;
      vecc(b, n);
      return 0;
    }
  }

  T = 60;
  b = cp;
  while (T--) {
    rotate(b.begin(), b.begin() + rng() % n, b.end());
    sig = 0;
    for (ll i = 0; i < n; i++) {
      if (a[i] == b[i]) {
        sig = 1;
        break;
      }
    }
    if (sig == 0) {
      cout << "Yes" << endl;
      vecc(b, n);
      return 0;
    }
  }

  cout << "No" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FLASH ios_base::sync_with_stdio(0);
#define ll long long
#define debt(x, y)                                                             \\
  cout << "#x = " << (x) << " and "                                            \\
       << "#y = " << (y) << endl;
#define deb(x) cout << "#x = " << (x) << endl;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define arr(a, n)                                                              \\
  for (ll i = 1; i <= n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";
#define vecc(a, n)                                                             \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";

using namespace std;

ll n;
vector<ll> a;
vector<ll> b;

ll T = 1000;

int main() {
  FLASH;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
  }

  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    b.pb(x);
  }

  ll sig = 0;

  for (ll i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      sig = 1;
      break;
    }
  }

  if (sig == 0) {
    cout << "Yes" << endl;
    vecc(b, n);
    return 0;
  }

  while (T--) {
    random_shuffle(all(b));
    sig = 0;
    for (ll i = 0; i < n; i++) {
      if (a[i] == b[i]) {
        sig = 1;
        break;
      }
    }
    if (sig == 0) {
      cout << "Yes" << endl;
      vecc(b, n);
      return 0;
    }
  }

  cout << "No" << endl;

  return 0;
}