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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5 + 42;
int a[N], b[N], ans[N], n;
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n;

  for (int i = 0; i < n; ++i)
    cin >> a[i];

  for (int i = 0; i < n; ++i)
    cin >> b[i];

  for (int i = 0; i < n; ++i)
    ans[i] = b[n - i - 1];

  int t;

  for (int i = 0; i < n; ++i) {
    if (ans[i] == a[i]) {
      for (int j = n - 1; j >= 0; --j) {
        if (ans[i] != a[j] && ans[j] != a[i]) {
          t = ans[i];
          ans[i] = ans[j];
          ans[j] = t;
          break;
        }
        if (j == 0) {
          cout << "No\\n";
          return 0;
        }
      }
    }
  }

  cout << "Yes\\n";

  for (int i = 0; i < n; ++i)
    cout << ans[i] << ' ';

  return 0;
}
/*

*/


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n = 0;

  cin >> n;

  vector<int> a(n), b(n);

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++)
    cin >> b[i];

  vector<int> ans(n);

  ans = b;

  reverse(all(ans));

  for (int i = 0; i < n; i++) {
    if (ans[i] == a[i]) {
      for (int j = 0; j < n; j++) {
        if (ans[i] != a[j] && ans[j] != a[i]) {
          swap(ans[i], ans[j]);
          break;
        }
        if (j == n - 1) {
          cout << "No\\n";
          return 0;
        }
      }
    }
  }

  cout << "Yes\\n";

  for (auto &x : ans)
    cout << x << ' ';

  return 0;
}
/*

*/