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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int long long
#define S second
#define F first
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define lb lower_bound
#define ub upper_bound
#define si(c) (int)((c).size())
#define lcm(a, b) (a * (b / __gcd(a, b)))
#define inf (int)(4e18)
// #define endl          '\\n'
#define mp make_pair
#define time(s) (double(clock() - s) / double(CLOCKS_PER_SEC))
#define debug(args...) _F(#args, args)
#define vi std::vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template <typename T> void _F(const char *name, T arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename T, typename... Args>
void _F(const char *names, T arg1, Args... args) {
  const char *name = strchr(names, ',');
  cerr.write(names, name - names) << " = " << arg1 << endl;
  _F(name + 2, args...);
}

template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &q) {
  in >> q.F >> q.S;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &q) {
  out << q.F << " " << q.S;
  return out;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F + p2.F, p1.S + p2.S};
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F - p2.F, p1.S - p2.S};
}
template <typename T1, typename T2>
bool operator<(pair<T1, T2> p1, pair<T1, T2> p2) {
  return p1 < p2;
}

template <typename T> void Unique(vector<T> &v) {
  sort(all(v)), v.resize(distance(v.begin(), unique(all(v))));
}

void solve() {
  int n;
  cin >> n;
  vi A(n), B(n), C(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> B[i];
    C[i] = B[i];
  }
  reverse(all(B));
  vi same;
  for (int i = 0; i < n; i++) {
    if (A[i] == B[i])
      same.pb(i);
  }
  auto print = [&]() {
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++)
      cout << B[i] << " ";
    exit(0);
  };
  if (si(same) == 0)
    print();
  int r = -1;
  for (int i = 0; i < n; i++) {
    if (A[i] == A[same[0]])
      r = i + 1;
    if (B[i] == A[same[0]])
      r = i + 1;
  }
  for (int i = 0; i < si(same); i++) {
    swap(B[same[i]], B[r % n]);
    r++;
  }
  for (int i = 0; i < n; i++) {
    if (A[i] == B[i]) {
      cout << "No" << endl;
      return;
    }
  }
  print();
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  start = clock();

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // freopen("error.txt", "w", stderr);
  // #endif

  int test = 1;
  // cin >> test;

  cout << fixed << setprecision(2);

  for (int i = 1; i <= test; ++i) {
    solve();
  }

  cerr << time(start);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int long long
#define S second
#define F first
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define lb lower_bound
#define ub upper_bound
#define si(c) (int)((c).size())
#define lcm(a, b) (a * (b / __gcd(a, b)))
#define inf (int)(4e18)
// #define endl          '\\n'
#define mp make_pair
#define time(s) (double(clock() - s) / double(CLOCKS_PER_SEC))
#define debug(args...) _F(#args, args)
#define vi std::vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template <typename T> void _F(const char *name, T arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename T, typename... Args>
void _F(const char *names, T arg1, Args... args) {
  const char *name = strchr(names, ',');
  cerr.write(names, name - names) << " = " << arg1 << endl;
  _F(name + 2, args...);
}

template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &q) {
  in >> q.F >> q.S;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &q) {
  out << q.F << " " << q.S;
  return out;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F + p2.F, p1.S + p2.S};
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F - p2.F, p1.S - p2.S};
}
template <typename T1, typename T2>
bool operator<(pair<T1, T2> p1, pair<T1, T2> p2) {
  return p1 < p2;
}

template <typename T> void Unique(vector<T> &v) {
  sort(all(v)), v.resize(distance(v.begin(), unique(all(v))));
}

const int N = 2e5 + 1;
int cntA[N], cntB[N];
int A[N], B[N];
vi ans[N];

void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    cntA[A[i]]++;
  }
  std::vector<pii> v;
  for (int i = 1; i <= n; i++) {
    if (cntA[i]) {
      v.pb(mp(cntA[i], i));
    }
  }
  sort(rall(v));
  set<pii> cur;
  for (int i = 0; i < n; i++) {
    cin >> B[i];
    cntB[B[i]]++;
  }
  cur.insert(mp(0, 0));
  for (int i = 1; i <= n; i++) {
    cur.insert(mp(cntB[i], i));
  }
  for (auto k : v) {
    int u = k.S;
    cur.erase(mp(cntB[u], u));
    while (cntA[u]) {
      pii p = *rbegin(cur);
      cur.erase(p);
      if (p.F == 0) {
        cout << "No" << endl;
        return;
      }
      int mn = min(p.F, cntA[u]);
      cntA[u] -= mn;
      cntB[p.S] -= mn;
      cur.insert(mp(cntB[p.S], p.S));
      while (mn)
        ans[u].pb(p.S);
    }
    cur.insert(mp(cntB[u], u));
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    while (si(ans[A[i]]) == 0)
      ;
    cout << ans[A[i]].back() << " ";
    ans[A[i]].pop_back();
  }
  cout << endl;
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  start = clock();

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // freopen("error.txt", "w", stderr);
  // #endif

  int test = 1;
  // cin >> test;

  cout << fixed << setprecision(2);

  for (int i = 1; i <= test; ++i) {
    solve();
  }

  cerr << time(start);
  return 0;
}