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

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int a[MAX], b[MAX];
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  queue<pair<int, int>> q;
  int r = n - 1;
  reverse(b, b + n);
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i])
      q.push({b[i], i});
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      int f = 0;
      int cnt = 0, sz = q.size();
      while (!q.empty()) {
        pair<int, int> cur = q.front();
        q.pop();
        int val = cur.first;
        int idx = cur.second;
        if (val != a[i] && b[i] != a[idx]) {
          swap(b[idx], b[i]);
          f = 1;
          q.push({val, idx});
          break;
        }
        q.push(cur);
        cnt++;
        if (cnt == sz)
          break;
      }
      if (!f) {
        cout << "No" << endl;
        return 0;
      }
      q.push({b[i], i});
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++)
    cout << b[i] << " ";
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int a[MAX], b[MAX];
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  queue<pair<int, int>> q;
  int r = n - 1;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      int f = 0;
      r = n - 1;
      while (r > i) {
        if (a[i] != b[r] && b[i] != a[r]) {
          f = 1;
          swap(b[i], b[r]);
          break;
        }
        r--;
      }
      if (!f) {
        int cnt = 0, sz = q.size();
        while (!q.empty()) {
          pair<int, int> cur = q.front();
          q.pop();
          int val = cur.first;
          int idx = cur.second;
          if (val != a[i] && b[i] != a[idx]) {
            swap(b[idx], b[i]);
            f = 1;
            q.push({val, idx});
            break;
          }
          q.push(cur);
          cnt++;
          if (cnt == sz)
            break;
        }
        if (!f) {
          cout << "No" << endl;
          return 0;
        }
      }
    }
    q.push({b[i], i});
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++)
    cout << b[i] << " ";
  cout << endl;

  return 0;
}