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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*
6
1 1 1 2 2 3
1 1 1 2 2 3
総数に対して半数をうわまわってなければいいか
1 1 1 2 2
2 2 3 3 2
1は2個までOK
2は3個までOK

queueにいれる
4 5 5 6
4 5 5 6

5 4 6 5
5 6 4 5
M[5]=2 [4]=1 [6]=1
queue {5, 4, 6}

[1]={0,2}
[2]={3,5}
[3]={6,8}

[1]={0,2} 2 - 0 + 1 = 3
[3]={3,5} 8 - 3 + 1 = 6
[4]={6,8} 0 - 6 + 1 = -5 = 0
1 1 1 2 2 2 3 3 3
1 1 1 3 3 3 4 4 4 1 1 1 3 3 3 4 4 4
|3    |6    |0
max = 6
4ずらし

*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  ll N;
  cin >> N;
  vector<pair<ll, ll>> A(200010, {-1, -1}), B(200010, {-1, -1});
  vector<ll> a(200010), b(200010);
  vector<ll> ka(N, 0), kb(N * 2, 0);
  ll a1 = 0;
  rep(i, N) {
    ll a2;
    cin >> a2;
    a[a2]++;
    ka[i] = a2;
    if (a1 != a2) {
      A[a1].second = i - 1;
      A[a2].first = i;
      a1 = a2;
    }
    if (i == N - 1)
      A[a2].second = i;
  }

  ll b1 = 0;
  rep(i, N) {
    ll b2;
    cin >> b2;
    b[b2]++;
    kb[i] = b2;
    if (b1 != b2) {
      B[b1].second = i - 1;
      B[b2].first = i;
      b1 = b2;
    }
    if (i == N - 1)
      B[b2].second = i;
  }
  rep(i, N) kb[i + N] = kb[i];
  rep(i, 200001) {
    if (a[i] + b[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  ll slide = 0;
  for (ll i = 1; i <= 200000; i++) {
    if (A[i].first == -1 || B[i].first == -1)
      continue;
    // cout << "i:"<< i << " a1:"<<A[i].first<<" a2:"<<A[i].second<<"
    // b1:"<<B[i].first<<" b2:"<<B[i].second<<endl;
    ll tmp = A[i].second - B[i].first + 1;
    chmax(slide, tmp);
  }
  // cout << "slide:"<< slide << endl;
  slide = N - slide;
  for (ll i = slide; i < N + slide; i++) {
    cout << kb[i];
    if (i == N + slide - 1)
      cout << endl;
    else
      cout << " ";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*
6
1 1 1 2 2 3
1 1 1 2 2 3
総数に対して半数をうわまわってなければいいか
1 1 1 2 2
2 2 3 3 2
1は2個までOK
2は3個までOK

*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  ll N;
  cin >> N;
  vector<ll> A(200010, 0), a(200010, 0), b(200010, 0);
  rep(i, N) {
    ll c;
    cin >> c;
    A[i] = c;
    a[c]++;
  }
  rep(i, N) {
    ll c;
    cin >> c;
    b[c]++;
  }
  rep(i, 200001) {
    if (N - a[i] < b[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  ll j = 0;
  while (N > 0) {
    for (ll i = 200000; i > 0; i--) {
      while (b[i] > 0 && A[j] != i && N > 0) {
        cout << i;
        if (N > 1)
          cout << " ";
        else
          cout << endl;
        b[i]--;
        N--;
        j++;
      }
    }
    for (ll i = 1; i <= 200000; i++) {
      while (b[i] > 0 && A[j] != i && N > 0) {
        cout << i;
        if (N > 1)
          cout << " ";
        else
          cout << endl;
        b[i]--;
        N--;
        j++;
      }
    }
  }
}