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

/*Read the problem carefully before starting to work on it*/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;

#define pb push_back
#define eb emplace_back
#define mp(x, y) make_pair(x, y)
#define mod 1000000007

double PI = 3.1415926535897932384626;

// template<typename T> T power(T x,T y,ll m=mod){T ans=1;while(y>0){if(y&1LL)
// ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ost;

// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// #define rnd(x, y) uniform_int_distribution<ll>(x, y)(rng)

// struct custom_hash {
//     static uint64_t splitmix64(uint64_t x) {
//         // http://xorshift.di.unimi.it/splitmix64.c
//         x += 0x9e3779b97f4a7c15;
//         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
//         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
//         return x ^ (x >> 31);
//     }
//     size_t operator()(uint64_t x) const {
//         static const uint64_t FIXED_RANDOM =
//             chrono::steady_clock::now().time_since_epoch().count();
//         return splitmix64(x + FIXED_RANDOM);
//     }
// };

#define fi first
#define se second

ll n;

bool check(vector<ll> &A, vector<ll> &B) {
  for (ll i = 0; i < n; i++) {
    if (A[i] == B[i])
      return true;
  }
  return false;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  vector<ll> A(n), B(n);
  map<ll, ll> mm;
  for (ll i = 0; i < n; i++) {
    cin >> A[i];
    mm[A[i]]++;
  }
  for (ll i = 0; i < n; i++) {
    cin >> B[i];
    mm[B[i]]++;
  }
  ll fl = 0;
  for (auto &x : mm) {
    if (x.se > n) {
      fl = 1;
      break;
    }
  }
  if (fl)
    cout << "No\\n";
  else {
    cout << "Yes\\n";
    // while (check(A, B))
    // {
    // 	random_shuffle(B.begin(), B.end());
    // }
    sort(B.rbegin(), B.rend());
    vector<ll> ans;
    for (ll i = 0; i < n; i++) {
      if (A[i] != B[i]) {
        ans.eb(B[i]);
        continue;
      }
      ll ree = n - 1;
      while (ree > i && A[i] == B[ree])
        ree--;
      if (B[i] != B[ree]) {
        swap(B[i], B[ree]);
        ans.eb(B[i]);
      } else {
        for (ll lul = 0; lul < i; lul++) {
          if ((A[lul] != B[i]) && (A[i] != ans[lul])) {
            ans.eb(B[i]);
            swap(ans[lul], ans[i]);
            break;
          }
        }
      }
    }
    for (auto &noo : ans) {
      cout << noo << " ";
    }
    cout << endl;
  }
  return 0;
}

// 252908XL

/*
comments:-

*/


Error: TLE

Fixed submission:

/*Read the problem carefully before starting to work on it*/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;

#define pb push_back
#define eb emplace_back
#define mp(x, y) make_pair(x, y)
#define mod 1000000007

double PI = 3.1415926535897932384626;

// template<typename T> T power(T x,T y,ll m=mod){T ans=1;while(y>0){if(y&1LL)
// ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ost;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rnd(x, y) uniform_int_distribution<ll>(x, y)(rng)

// struct custom_hash {
//     static uint64_t splitmix64(uint64_t x) {
//         // http://xorshift.di.unimi.it/splitmix64.c
//         x += 0x9e3779b97f4a7c15;
//         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
//         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
//         return x ^ (x >> 31);
//     }
//     size_t operator()(uint64_t x) const {
//         static const uint64_t FIXED_RANDOM =
//             chrono::steady_clock::now().time_since_epoch().count();
//         return splitmix64(x + FIXED_RANDOM);
//     }
// };

#define fi first
#define se second

ll n;

bool check(vector<ll> &A, vector<ll> &B) {
  for (ll i = 0; i < n; i++) {
    if (A[i] == B[i])
      return true;
  }
  return false;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  vector<ll> A(n), B(n);
  map<ll, ll> mm;
  for (ll i = 0; i < n; i++) {
    cin >> A[i];
    mm[A[i]]++;
  }
  for (ll i = 0; i < n; i++) {
    cin >> B[i];
    mm[B[i]]++;
  }
  ll fl = 0;
  for (auto &x : mm) {
    if (x.se > n) {
      fl = 1;
      break;
    }
  }
  if (fl)
    cout << "No\\n";
  else {
    cout << "Yes\\n";
    // while (check(A, B))
    // {
    // 	random_shuffle(B.begin(), B.end());
    // }
    sort(B.rbegin(), B.rend());
    for (ll i = 0; i < n; i++) {
      if (A[i] != B[i]) {
        cout << B[i] << " ";
        continue;
      }
      ll ree = n - 1;
      while (A[i] == B[ree])
        ree = rnd(i + 1, n - 1);
      swap(B[i], B[ree]);
      cout << B[i] << " ";
    }
    cout << endl;
  }
  return 0;
}

// 252908XL

/*
comments:-

*/