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

// For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456
// a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second

using namespace std;

vector<int> v[200001];

int main() {
  std::ios::sync_with_stdio(false);
  int T;
  T = 1;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    int n, i, j;
    cin >> n;
    int ar[n + 1], ar2[n + 1];
    map<int, int> mp, mp2, mp3;
    for (i = 1; i <= n; ++i) {
      cin >> ar[i];
      ++mp[ar[i]];
    }
    for (i = 1; i <= n; ++i) {
      cin >> ar2[i];
      ++mp2[ar2[i]];
      ++mp3[ar2[i]];
    }
    for (auto it : mp) {
      if (it.s + mp2[it.f] > n) {
        cout << "No" << endl;
        return 0;
      }
    }
    shuffle(ar2 + 1, ar2 + n + 1, rng);
    if (mp.size() <= 10) {
      sort(ar2 + 1, ar2 + n, greater<int>());
    }
    int ans[n + 1];

    for (i = 1; i <= n; ++i)
      ans[i] = ar2[i];
    for (i = 1; i <= n; ++i) {
      if (ans[i] != ar[i])
        continue;
      for (j = 1; j <= n; ++j) {
        if (i == j)
          continue;
        if (ar[j] != ans[i] && ans[j] != ar[i]) {
          swap(ans[i], ans[j]);
          break;
        }
      }
    }
    cout << "Yes" << endl;
    for (i = 1; i <= n; ++i)
      cout << ans[i] << " ";
    cout << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456
// a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second

using namespace std;

vector<int> v[200001];

int main() {
  std::ios::sync_with_stdio(false);
  int T;
  T = 1;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    int n, i, j;
    cin >> n;
    int ar[n + 1], ar2[n + 1];
    map<int, int> mp, mp2, mp3;
    for (i = 1; i <= n; ++i) {
      cin >> ar[i];
      ++mp[ar[i]];
    }
    for (i = 1; i <= n; ++i) {
      cin >> ar2[i];
      ++mp2[ar2[i]];
      ++mp3[ar2[i]];
    }
    for (auto it : mp) {
      if (it.s + mp2[it.f] > n) {
        cout << "No" << endl;
        return 0;
      }
    }
    int ans[n + 1];
    shuffle(ar2 + 1, ar2 + n + 1, rng);
    for (i = 1; i <= n; ++i)
      ans[i] = ar2[i];
    for (i = 1; i <= n; ++i) {
      if (ans[i] != ar[i])
        continue;
      for (j = 1; j <= n; ++j) {
        if (i == j)
          continue;
        if (ar[j] != ans[i] && ans[j] != ar[i]) {
          swap(ans[i], ans[j]);
          break;
        }
      }
    }
    cout << "Yes" << endl;
    for (i = 1; i <= n; ++i)
      cout << ans[i] << " ";
    cout << endl;
  }
  return 0;
}