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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  vector<int> cntA(N + 1), cntB(N + 1);
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    A[i] = x;
    ++cntA[x];
  }
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    B[i] = x;
    ++cntB[x];
  }

  for (int i = 1; i < N + 1; ++i) {
    if (cntA[i] + cntB[i] > N) {
      cout << "No" << '\\n';
      return;
    }
  }

  cout << "Yes" << '\\n';
  int shift = 0;
  int sumA = 0, sumB = 0;
  for (int i = 1; i < N + 1; ++i) {
    sumA += cntA[i];
    chmax(shift, sumA - sumB);
    sumB += cntB[i];
  }
  for (int i = 0; i < N; ++i) {
    cout << B[(N + i - shift) % N];
    if (i == N - 1)
      cout << '\\n';
    else
      cout << ' ';
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int N;
  cin >> N;
  vector<int> A(N), cntA(N + 1), cntB(N + 1);
  queue<int> B;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    ++cntA[A[i]];
  }
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    ++cntB[x];
    B.push(x);
  }

  for (int i = 1; i < N + 1; ++i) {
    if (cntA[i] + cntB[i] > N) {
      cout << "No" << '\\n';
      return;
    }
  }

  queue<int> rem;
  vector<int> ans(N, -1);
  for (int i = N - 1; i >= 0 && !B.empty(); --i) {
    if (cntA[A[i]] > 0) {
      while (!B.empty()) {
        int x = B.front();
        B.pop();
        if (x == A[i]) {
          rem.push(x);
        } else {
          ans[i] = x;
          break;
        }
      }
    }
  }
  while (!rem.empty()) {
    B.swap(rem);
    for (int i = 0; i < N; ++i) {
      if (ans[i] == -1) {
        while (!B.empty()) {
          int x = B.front();
          B.pop();
          if (x == A[i]) {
            rem.push(x);
          } else {
            ans[i] = x;
            break;
          }
        }
      }
    }
  }
  cout << "Yes" << '\\n';
  for (int i = 0; i < N; ++i) {
    cout << ans[i];
    if (i == N - 1)
      cout << '\\n';
    else
      cout << ' ';
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}