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

#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse4,sse4.2,popcnt,abm,mmx,avx")
#endif
#include <bits/stdc++.h>

using namespace std;

#define popCnt(x) (__builtin_popcountll(x))
#define sz(x) ((int)(x.size()))
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
typedef long long Long;
typedef double Double;

template <class U, class V> istream &operator>>(istream &is, pair<U, V> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &x : v) {
    os << x << " ";
  }
  return os;
}

vector<int> a, b, res;

int n;

map<int, int> cnt_a, cnt_b;

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

bool validate() {
  bool valid = true;
  for (int i = 0; i < n; ++i) {
    valid &= (a[i] != res[i]);
  }
  return valid;
}

bool solve() {
  res = b;
  for (int t = 0; t < 300; ++t) {
    rotate(begin(res), begin(res) + rng() % n, end(res));
    if (validate())
      return true;
  }
  res = b;
  reverse(all(res));
  for (int t = 0; t < 300; ++t) {
    if (validate())
      return true;
    shuffle(all(res), rng);
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\\n'
#endif
  cin >> n;
  a.resize(n), b.resize(n), res.resize(n);
  cin >> a >> b;
  for (int x : b)
    ++cnt_b[x];
  for (int x : a) {
    ++cnt_a[x];
    if (cnt_b.count(x) && cnt_b[x] + cnt_a[x] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  if (solve()) {
    cout << "Yes" << endl;
    cout << res << endl;
    return 0;
  }

  cout << "No" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse4,sse4.2,popcnt,abm,mmx,avx")
#endif
#include <bits/stdc++.h>

using namespace std;

#define popCnt(x) (__builtin_popcountll(x))
#define sz(x) ((int)(x.size()))
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
typedef long long Long;
typedef double Double;

template <class U, class V> istream &operator>>(istream &is, pair<U, V> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &x : v) {
    os << x << " ";
  }
  return os;
}

vector<int> a, b, res;

int n;

map<int, int> cnt_a, cnt_b;

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

bool validate() {
  bool valid = true;
  for (int i = 0; i < n; ++i) {
    valid &= (a[i] != res[i]);
  }
  return valid;
}

bool solve() {
  res = b;
  reverse(all(res));
  for (int t = 0; t < 350; ++t) {
    if (validate())
      return true;
    shuffle(all(res), rng);
  }
  res = b;
  for (int t = 0; t < 350; ++t) {
    rotate(begin(res), begin(res) + rng() % n, end(res));
    if (validate())
      return true;
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\\n'
#endif
  cin >> n;
  a.resize(n), b.resize(n), res.resize(n);
  cin >> a >> b;
  for (int x : b)
    ++cnt_b[x];
  for (int x : a) {
    ++cnt_a[x];
    if (cnt_b.count(x) && cnt_b[x] + cnt_a[x] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  if (solve()) {
    cout << "Yes" << endl;
    cout << res << endl;
    return 0;
  }

  cout << "No" << endl;
  return 0;
}