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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// same # > N => Ai=Bi always exists
bool judge(vector<int> &CA, vector<int> &CB, int N) {
  for (int i = 0; i <= N; ++i) {
    if (CA[i] + CB[i] > N)
      return false;
  }
  return true;
}

// CA[i], CB[i]: count (element < i)
// condition of x:
// all i in [1,N];
// for 3 sections (following) S1..S3, (S1^S2)v(S2^S3)v(S3^S1) = null
// S1 = ( CA[i-1], CA[i] ]
// S2 = ( x+CB[i-1], x+CB[i] ]
// S3 = ( N+CA[i-1], N+CA[i] ]
// <=> all i in [1,N]; CA[i] - CB[i-1] <= x <= N + CA[i-1] - CB[i]

int B_shift(vector<int> &CA, vector<int> &CB, int N) {
  vector<int> TA(N + 1, 0), TB(N + 1, 0);
  for (int i = 1; i <= N; ++i) {
    TA[i] = CA[i] + TA[i - 1];
    TB[i] = CB[i] + TB[i - 1];
  }
  int xmax = 0;
  for (int i = 1; i <= N; ++i) {
    xmax = max(xmax, TA[i] - TB[i - 1]);
  }
  return xmax;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  vector<int> CA(N + 1, 0), CB(N + 1, 0); // # of Ai/Bi = n
  rep(i, N) {
    cin >> A[i]; // Ai,Bi = 1...N, Ai,Bi are sorted
    ++CA[A[i]];
  }
  rep(i, N) {
    cin >> B[i]; // Ai,Bi = 1...N, Ai,Bi are sorted
    ++CB[B[i]];
  }
  bool tf = judge(CA, CB, N);
  if (!tf)
    cout << "No" << endl;
  if (tf) {
    cout << "Yes" << endl;
    // construct B_purmutation by shift B by x;
    int x = B_shift(CA, CB, N);
    rep(i, N) {
      if (i)
        cout << " ";
      cout << B[(i - x + N) % N];
    }
    cout << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// same # > N => Ai=Bi always exists
bool judge(vector<int> &A, vector<int> &B, int N) {
  for (int i = 1; i <= N; ++i) {
    int total = 0;
    total += count(A.begin(), A.end(), i);
    total += count(B.begin(), B.end(), i);
    if (total > N)
      return false;
  }
  return true;
}

// CA[i], CB[i]: count (element < i)
// condition of x:
// all i in [1,N];
// for 3 sections (following) S1..S3, (S1^S2)v(S2^S3)v(S3^S1) = null
// S1 = ( CA[i-1], CA[i] ]
// S2 = ( x+CB[i-1], x+CB[i] ]
// S3 = ( N+CA[i-1], N+CA[i] ]
// <=> all i in [1,N]; CA[i] - CB[i-1] <= x <= N + CA[i-1] - CB[i]

int B_shift(vector<int> &A, vector<int> &B, int N) {
  vector<int> CA(N + 1, 0), CB(N + 1, 0);
  rep(i, N) {
    for (int j = N; j >= A[i]; --j)
      ++CA[j];
    for (int j = N; j >= B[i]; --j)
      ++CB[j];
  }
  int diffmax = 0;
  for (int i = 1; i <= N; ++i) {
    int diff = CA[i] - CB[i - 1];
    diffmax = max(diffmax, diff);
  }
  return diffmax;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i]; // Ai,Bi = 1...N => 0...N-1
  rep(i, N) cin >> B[i]; // Ai,Bi are sorted

  bool tf = judge(A, B, N);
  if (!tf)
    cout << "No" << endl;
  if (tf) {
    cout << "Yes" << endl;
    // construct B_purmutation by shift B by x;
    int x = B_shift(A, B, N);
    rep(i, N) {
      if (i)
        cout << " ";
      cout << B[(i - x + N) % N];
    }
    cout << endl;
  }
  return 0;
}