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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  reverse(all(b));
  map<int, int> x;
  rep(i, n) {
    x[a[i]]++;
    x[b[i]]++;
  }
  rep(i, n) {
    if (x[b[i]] > n) {
      No;
      return 0;
    }
  }
  Yes;
  bool f = true;
  int st, fn;
  rep(i, n) {
    if (a[i] == b[i] && f) {
      f = false;
      st = i;
      fn = i;
    } else if (a[i] == b[i]) {
      fn = i;
    } else if (!f) {
      break;
    }
  }
  if (f) {
    rep(i, n) cout << b[i] << " ";
    cout << endl;
    return 0;
  }

  rep(i, n) {
    if (st > fn)
      break;
    if (a[i] == b[st])
      continue;
    if (b[i] == a[st])
      continue;
    if (st <= i && i <= fn)
      continue;
    swap(b[i], b[st]);
    st++;
  }
  rep(i, n) cout << b[i] << " ";
  cout << endl;
} /*
 ./problem.exe
 */

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  vector<int> b(n);
  rep(i, n) cin >> b.at(i);
  vector<int> ans(n, 0);
  map<int, int> acnt, bcnt, yabasa, ast, bst;
  rep(i, n) {
    if (acnt[a[i]] == 0)
      ast[a[i]] = i;
    acnt[a[i]]++;
    if (bcnt[b[i]] == 0)
      bst[b[i]] = i;
    bcnt[b[i]]++;
  }
  int x, y;
  for (auto z : acnt) {
    tie(x, y) = z;
    yabasa[x] = y;
  }
  for (auto z : bcnt) {
    tie(x, y) = z;
    yabasa[x] += y;
  }
  priority_queue<P> que;
  for (auto z : yabasa) {
    tie(x, y) = z;
    que.emplace(y, x);
  }
  bool ikeru = true;
  int nokori = n;
  int yuto = 0;
  int yutost = 0;
  int aki = 0;

  while (!que.empty() && ikeru && nokori > 0) {
    tie(x, y) = que.top();
    que.pop();

    if (nokori - acnt[y] < bcnt[y]) {
      ikeru = false;
      break;
    }
    if (nokori <= yuto) {
      while (bcnt[y]) {
        bcnt[y]--;
        REP(i, yutost, n) {
          if (ans[i] == 0) {
            yutost = i + 1;
            ans[i] = y;
            nokori--;
            yuto--;
            break;
          }
        }
      }
      continue;
    }
    if (x == 0) {
      while (bcnt[y]) {
        bcnt[y]--;
        REP(i, aki, n) {
          if (ans[i] == 0) {
            aki = i + 1;
            ans[i] = y;
            nokori--;
            break;
          }
        }
      }
      continue;
    }
    int z, w;
    tie(z, w) = que.top();
    que.pop();

    if (z == 0) {
      que.emplace(-1, y);
      que.emplace(z, w);
    }

    int yaru = min(acnt[w], bcnt[y]);
    int st = ast[w];
    rep(i, yaru) { ans[st + i] = y; }
    ast[w] += yaru;
    nokori -= yaru;
    x -= yaru;
    z -= yaru;
    acnt[w] -= yaru;
    bcnt[y] -= yaru;
    if (bcnt[y] > 0)
      que.emplace(x, y);
    else
      yuto += acnt[y];
    if (acnt[w] > 0)
      que.emplace(z, w);
    else
      que.emplace(0, w);
  }

  if (!ikeru) {
    No;
    return 0;
  }
  Yes;
  rep(i, n) { cout << ans[i] << " "; }
  cout << endl;
} /*
 ./problem.exe
 */