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;
bool solve1() {
  multiset<int> ms_b(all(b));
  for (int i = 0; i < n; ++i) {
    auto it = begin(ms_b);
    if (*it <= a[i]) {
      ms_b.insert(*it + n);
      ms_b.erase(begin(ms_b));
      --i;
      continue;
    }
    res[i] = *it;
    ms_b.erase(it);
    if (res[i] > n)
      res[i] -= n;
    if (a[i] == res[i])
      return false;
  }
  return true;
}
map<int, int> cnt_a, cnt_b;

bool solve2() {
  set<pair<int, int>> st;
  for (auto &x : cnt_b) {
    st.emplace(x.second, x.first);
  }
  for (int i = 0; i < n; ++i) {
    auto it = end(st);
    --it;
    if (it->second == a[i]) {
      if (it == begin(st))
        return false;
      --it;
    }
    res[i] = it->second;
    auto tmp = *it;
    st.erase(it);
    --tmp.first;
    if (tmp.first != 0) {
      st.insert(tmp);
    }
  }
  return true;
}
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 solve3() {
  res = b;
  reverse(all(res));
  for (int t = 0; t < 150; ++t) {
    if (validate())
      return true;
    shuffle(all(res), rng);
  }
  res = b;
  for (int t = 0; t < 150; ++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;
    }
  }

  bool (*habds[])() = {solve1, solve2, solve3};
  for (auto habd : habds) {
    if (habd()) {
      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;
bool solve1() {
  multiset<int> ms_b(all(b));
  for (int i = 0; i < n; ++i) {
    auto it = begin(ms_b);
    if (*it <= a[i]) {
      ms_b.insert(*it + n);
      ms_b.erase(begin(ms_b));
      --i;
      continue;
    }
    res[i] = *it;
    ms_b.erase(it);
    if (res[i] > n)
      res[i] -= n;
    if (a[i] == res[i])
      return false;
  }
  return true;
}
map<int, int> cnt_a, cnt_b;

bool solve2() {
  set<pair<int, int>> st;
  for (auto &x : cnt_b) {
    st.emplace(x.second, x.first);
  }
  for (int i = 0; i < n; ++i) {
    auto it = end(st);
    --it;
    if (it->second == a[i]) {
      if (it == begin(st))
        return false;
      --it;
    }
    res[i] = it->second;
    auto tmp = *it;
    st.erase(it);
    --tmp.first;
    if (tmp.first != 0) {
      st.insert(tmp);
    }
  }
  return true;
}
const int RANDOM =
    chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);

bool solve3() {
  res = b;
  vector<int> perm(n);
  iota(all(perm), 0);
  for (int t = 0; t < 270; ++t) {
    shuffle(all(res), rng);
    shuffle(all(perm), rng);
    bool valid = true;
    for (int i = 0; i < n; ++i) {
      valid &= (a[perm[i]] != res[perm[i]]);
    }
    if (valid)
      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_a[x] > n / 2 && cnt_b.count(x) && cnt_b[x] > n / 2) {
      cout << "No" << endl;
      return 0;
    }
  }

  bool (*habds[])() = {solve1, solve2, solve3};
  for (auto habd : habds) {
    if (habd()) {
      cout << "Yes" << endl;
      cout << res << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}