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

/*input

*/

// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space #pragma GCC target ("avx2") #pragma GCC
// optimization ("O3") #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
using namespace __gnu_pbds;
template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long int
#define pb push_back
#define pii pair<ll, ll>
#define vpii vector<pii>
#define vi vector<ll>
#define vs vector<string>
#define vvi vector<vector<ll>>
#define inf (ll)1e18
#define all(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define F first
#define S second
#define sz(x) (ll) x.size()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define mp make_pair
#define whatis(x) cout << #x << " is " << x << "\\n";
#define graph(n) adj(n, vector<ll>())
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define debug(x) cout << #x << " " << x << endl;
#define debug_p(x) cout << #x << " " << x.F << " " << x.S << endl;
#define debug_v(x)                                                             \\
  {                                                                            \\
    cout << #x << " ";                                                         \\
    for (auto ioi : x)                                                         \\
      cout << ioi << " ";                                                      \\
    cout << endl;                                                              \\
  }
#define debug_vp(x)                                                            \\
  {                                                                            \\
    cout << #x << " ";                                                         \\
    for (auto ioi : x)                                                         \\
      cout << '[' << ioi.F << " " << ioi.S << ']';                             \\
    cout << endl;                                                              \\
  }
#define debug_v_v(x)                                                           \\
  {                                                                            \\
    cout << #x << "/*\\n";                                                      \\
    for (auto ioi : x) {                                                       \\
      for (auto ioi2 : ioi)                                                    \\
        cout << ioi2 << " ";                                                   \\
      cout << '\\n';                                                            \\
    }                                                                          \\
    cout << "*/" << #x << endl;                                                \\
  }

int solve() {
  ll n;
  cin >> n;

  vi a(n);
  rep(i, 0, n) cin >> a[i];

  vi b(n);
  rep(i, 0, n) cin >> b[i];

  reverse(b.begin(), b.end());
  ll L = n + 1, R = -1;
  rep(i, 0, n) {
    if (a[i] == b[i]) {
      L = min(L, i);
      R = max(R, i);
    }
  }

  rep(j, 0, n) {
    if (L <= R && a[j] != a[L] && b[j] != a[L]) {
      swap(b[j], b[L++]);
    }
  }

  if (L <= R) {
    cout << "No\\n";
  } else {
    cout << "Yes\\n";
    rep(i, 0, n) cout << b[i] << " ";
  }

  return 0;
}

int main() {
  auto start = chrono::high_resolution_clock::now();

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll test_cases = 1;
  // cin>>test_cases;
  while (test_cases--)
    solve();

  auto stop = chrono::high_resolution_clock::now();
  auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
  // cout<<"\\nduration: "<<(double)duration.count()<<" milliseconds";
}

Error: TLE

Fixed submission:

/*input

*/

// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space #pragma GCC target ("avx2") #pragma GCC
// optimization ("O3") #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
using namespace __gnu_pbds;
template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long int
#define pb push_back
#define pii pair<ll, ll>
#define vpii vector<pii>
#define vi vector<ll>
#define vs vector<string>
#define vvi vector<vector<ll>>
#define inf (ll)1e18
#define all(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define F first
#define S second
#define sz(x) (ll) x.size()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define mp make_pair
#define whatis(x) cout << #x << " is " << x << "\\n";
#define graph(n) adj(n, vector<ll>())
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define debug(x) cout << #x << " " << x << endl;
#define debug_p(x) cout << #x << " " << x.F << " " << x.S << endl;
#define debug_v(x)                                                             \\
  {                                                                            \\
    cout << #x << " ";                                                         \\
    for (auto ioi : x)                                                         \\
      cout << ioi << " ";                                                      \\
    cout << endl;                                                              \\
  }
#define debug_vp(x)                                                            \\
  {                                                                            \\
    cout << #x << " ";                                                         \\
    for (auto ioi : x)                                                         \\
      cout << '[' << ioi.F << " " << ioi.S << ']';                             \\
    cout << endl;                                                              \\
  }
#define debug_v_v(x)                                                           \\
  {                                                                            \\
    cout << #x << "/*\\n";                                                      \\
    for (auto ioi : x) {                                                       \\
      for (auto ioi2 : ioi)                                                    \\
        cout << ioi2 << " ";                                                   \\
      cout << '\\n';                                                            \\
    }                                                                          \\
    cout << "*/" << #x << endl;                                                \\
  }

int solve() {
  ll n;
  cin >> n;
  vi a(n), b(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];

  ll pos = 1;
  rep(i, 0, n) if (a[i] == b[i]) pos = 0;
  if (pos == 1) {
    cout << "Yes\\n";
    rep(i, 0, n) cout << b[i] << " ";
  } else {
    set<ll> equalIdx, notEqualIdx;
    map<ll, ll> wh;
    rep(i, 0, n) if (a[i] == b[i]) equalIdx.insert(i), wh[a[i]]++;
    else notEqualIdx.insert(i);

    ll el = -1;
    all(it, wh) if (it->S > (sz(equalIdx) / 2)) el = it->F;
    // whatis(el);
    if (el != -1) {
      // ll difReq = wh[el] - sz(equalIdx)/2;
      // whatis(difReq);
      map<ll, ll> cnt1, cnt2;
      vector<set<ll>> posi(n + 1);
      rep(i, 0, n) {
        if (a[i] != b[i]) {
          cnt1[a[i]]++;
          cnt2[b[i]]++;
          posi[b[i]].insert(i);
        }
      }

      rep(i, 0, n) {
        if (a[i] != el && a[i] != b[i] && wh[el] > sz(equalIdx) / 2 &&
            cnt2[a[i]] > 0) {
          // difReq--;
          cnt2[a[i]]--;
          // cout<<"ok\\n";
          ll p = *posi[a[i]].begin();

          posi[a[i]].erase(p);
          posi[b[i]].erase(i);

          swap(b[p], b[i]);

          posi[b[p]].insert(p);
          posi[b[i]].insert(i);

          assert(b[i] == a[i]);
          posi[b[i]].erase(i);
          equalIdx.insert(i);
        }
      }

      if (wh[el] > sz(equalIdx) / 2) {
        vi temp(n);
        rep(k, 1, 10000) {
          ll ok = 1;
          rep(i, 0, n) {
            ll im = (i + k) % n;
            temp[i] = b[im];
            if (temp[i] == a[i])
              ok = 0;
          }

          if (ok == 1) {
            cout << "Yes\\n";
            rep(i, 0, n) cout << temp[i] << " ";
            return 0;
          }
        }

        rep(i, 0, n) {
          ll im = (i + n / 2) % n;
          temp[i] = b[im];
        }

        rep(i, 0, n) {
          if (temp[i] == a[i]) {
            cout << "No\\n";
            return 0;
          }
        }

        cout << "Yes\\n";
        rep(i, 0, n) cout << temp[i] << " ";
        return 0;
      }
    }

    vi temp;
    wh.clear();

    all(it, equalIdx) temp.pb(*it), wh[*it]++;

    el = (*wh.begin()).F;
    all(it, wh) if (it->S > wh[el]) el = it->F;

    ll k = sz(temp) / 2;
    vi ans(b.begin(), b.end());
    rep(i, 0, sz(temp)) {
      ll im = (i + k) % sz(temp);
      ans[temp[i]] = b[temp[im]];
    }

    rep(i, 0, n) assert(ans[i] != a[i]);

    cout << "Yes\\n";
    rep(i, 0, n) cout << ans[i] << " ";
  }

  return 0;
}

int main() {
  auto start = chrono::high_resolution_clock::now();

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll test_cases = 1;
  // cin>>test_cases;
  while (test_cases--)
    solve();

  auto stop = chrono::high_resolution_clock::now();
  auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
  // cout<<"\\nduration: "<<(double)duration.count()<<" milliseconds";
}