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

#include <algorithm>
#include <experimental/random>
#include <iostream>
#include <vector>

using namespace std;
#ifdef __DEBUG__
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int M;
vector<int> X, Y;
inline void input() {
  fast_io();
  cin >> M;
  X = fetch_vec<int>(M);
  Y = fetch_vec<int>(M);
}
void chmax(int &a, int b) {
  if (a < b)
    a = b;
};

inline int solve(int N, vector<int> A, vector<int> B) {
  vector<int> cntA(N + 1, 0), cntB(N + 1, 0);
  for (int x : A)
    cntA[x]++;
  for (int x : B)
    cntB[x]++;
  for (int i = 1; i <= N; i++) {
    if ((cntA[i] + cntB[i]) > N) {
      cout << "No"
           << "\\n";
      return 0;
    }
  }
  vector<int> IA = cntA, IB = cntB;
  for (int i = 0; i < N; i++) {
    IA[i + 1] += IA[i];
    IB[i + 1] += IB[i];
  }

  int x = 0;
  for (int k = 1; k <= N; k++) {
    if (cntA[k] == 0 || cntB[k] == 0)
      continue;
    chmax(x, IA[k] - IB[k - 1]);
  }

  cout << "Yes"
       << "\\n";
  // xだけBをずらす.
  for (int i = 0; i < N; i++) {
    cout << B[(N + i - x) % N];
    if (i != N - 1)
      cout << " ";
  }
  cout << "\\n";

  return 0;
}

int test() {
  int n = 5;
  vector<int> a(n);
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    a[i] = std::experimental::randint(1, n);
    b[i] = std::experimental::randint(1, n);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  debug(n);
  debug(a);
  debug(b);
  solve(n, a, b);
  return 0;
}

int main() {
  // int t = 10;
  // while(t--) test();
  input();
  solve(M, X, Y);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <experimental/random>
#include <iostream>
#include <vector>

using namespace std;
#ifdef __DEBUG__
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int M;
vector<int> X, Y;
inline void input() {
  fast_io();
  cin >> M;
  X = fetch_vec<int>(M);
  Y = fetch_vec<int>(M);
}

inline int solve(int N, vector<int> A, vector<int> B) {
  vector<int> cntA(N + 1, 0), cntB(N + 1, 0);
  for (int x : A)
    cntA[x]++;
  for (int x : B)
    cntB[x]++;
  for (int i = 1; i <= N; i++) {
    if ((cntA[i] + cntB[i]) > N) {
      cout << "No"
           << "\\n";
      return 0;
    }
  }
  bool flagN = false;
  int x = 0;
  for (int i = 1; i <= N; i++) {
    if ((cntA[i] + cntB[i]) == N) {
      flagN = true;
      x = i;
      break;
    }
  }
  vector<int> Ans;

  if (flagN) {
    vector<int> BX;
    for (int i = 0; i < N; i++) {
      if (B[i] != x) {
        BX.push_back(B[i]);
      }
    }
    int j = 0;
    for (int i = 0; i < N; i++) {
      if (A[i] == x) {
        Ans.push_back(BX[j]);
        j++;
      } else {
        Ans.push_back(x);
      }
    }
    cout << "Yes"
         << "\\n";
    cout << Ans << endl;
  } else {
    // auto cmp_b = [&](int x, int y){
    //     if(cntA[x]+cntB[x] == cntA[y]+cntB[y]) return cntA[x]+cntB[x] >
    //     cntA[y]+cntB[y]; return cntA[x] > cntA[y];
    // };
    // sort(B.begin(),B.end(),cmp_b);
    reverse(B.begin(), B.end());
    bool rev = true;
    for (int i = 0; i < N; i++) {
      if (A[i] == B[i]) {
        rev = false;
        break;
      }
    }
    if (rev) {
      cout << "Yes"
           << "\\n";
      cout << B << endl;
      return 0;
    }

    std::mt19937 get_rand_mt;
    std::shuffle(B.begin(), B.end(), get_rand_mt);
    // debug(B);
    vector<bool> usedB(N, false);
    int j = 0;
    int times = 0;
    for (int i = 0; i < N; i++) {
      while (A[i] == B[j] || usedB[j]) {
        j++;
        if (j >= N)
          times++, j %= N;
      }
      Ans.push_back(B[j]);
      usedB[j] = true;
    }
    cout << "Yes"
         << "\\n";
    cout << Ans << endl;
  }
  return 0;
}

int test() {
  int n = 10;
  vector<int> a(n);
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    a[i] = std::experimental::randint(1, n);
    b[i] = std::experimental::randint(1, n);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  debug(n);
  debug(a);
  debug(b);
  solve(n, a, b);
  return 0;
}

int main() {
  // int t = 10;
  // while(t--) test();
  input();
  solve(M, X, Y);
  return 0;
}