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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// chmax, chmin
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const string YES = "Yes";
const string NO = "No";

void solve(long long N, std::vector<long long> A, std::vector<long long> B) {

  map<int, pair<int, int>> memo;
  map<int, vector<int>> pos;
  for (int i = 0; i < B.size(); i++) {
    memo[B[i]].second++;
  }
  for (int i = 0; i < A.size(); i++) {
    memo[A[i]].first++;
    pos[A[i]].push_back(i);
  }
  vector<pair<int, pair<int, int>>> c;
  for (auto i : memo) {
    c.push_back({i.first, i.second});
  }

  int rot = 0;
  auto f = [&]() {
    vector<pair<int, int>> matchings;
    multiset<int> cands;
    for (auto y : c) {
      if (y.second.second + y.second.first > N) {
        cout << "No" << endl;
        exit(0);
      }
      for (int i = 0; i < y.second.first; i++) {
        cands.insert(y.first);
      }
    }

    sort(c.begin(), c.end(),
         [&](pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
           return max(a.second.first, a.second.second) >
                  max(b.second.first, b.second.second);
         });
    auto it = cands.begin();

    rotate(c.begin(), c.begin() + rot, c.end());
    for (auto y : c) {
      int crash = 0;
      int fail = 0;
      for (int i = 0; i < y.second.second; i++) {
        if (it == cands.end()) {
          it = cands.begin();
          crash++;
          if (crash > 3) {
            return;
          }
        }
        if (*it != y.first) {
          matchings.push_back({*it, y.first});
          it = cands.erase(it);
        } else {
          ++it;
          --i;
        }
      }
    }

    vector<int> BX(N);
    for (auto i : matchings) {
      BX[pos[i.first].back()] = i.second;
      pos[i.first].pop_back();
    }
    cout << "Yes" << endl;
    for (auto i : BX) {
      cout << i << " ";
    }
    cout << endl;
    exit(0);
  };

  while (true) {
    f();
    rot++;
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  std::vector<long long> B(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &B[i]);
  }
  solve(N, std::move(A), std::move(B));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// chmax, chmin
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const string YES = "Yes";
const string NO = "No";

void solve(long long N, std::vector<long long> A, std::vector<long long> B) {

  map<int, pair<int, int>> memo;
  map<int, vector<int>> pos;
  for (int i = 0; i < B.size(); i++) {
    memo[B[i]].second++;
  }
  for (int i = 0; i < A.size(); i++) {
    memo[A[i]].first++;
    pos[A[i]].push_back(i);
  }
  vector<pair<int, pair<int, int>>> c;
  for (auto i : memo) {
    c.push_back({i.first, i.second});
  }

  vector<pair<int, int>> matchings;
  multiset<int> cands;
  for (auto y : c) {
    if (y.second.second + y.second.first > N) {
      cout << "No" << endl;
      return;
    }
    for (int i = 0; i < y.second.first; i++) {
      cands.insert(y.first);
    }
  }

  sort(c.begin(), c.end(),
       [&](pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
         return max(a.second.first, a.second.second) >
                max(b.second.first, b.second.second);
       });
  auto it = cands.begin();
  for (auto y : c) {
    for (int i = 0; i < y.second.second; i++) {
      if (it == cands.end()) {
        it = cands.begin();
      }
      if (*it != y.first) {
        matchings.push_back({*it, y.first});
        it = cands.erase(it);
      } else {
        ++it;
        --i;
      }
    }
  }

  vector<int> BX(N);
  for (auto i : matchings) {
    BX[pos[i.first].back()] = i.second;
    pos[i.first].pop_back();
  }
  cout << "Yes" << endl;
  for (auto i : BX) {
    cout << i << " ";
  }
  cout << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  std::vector<long long> B(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &B[i]);
  }
  solve(N, std::move(A), std::move(B));
  return 0;
}