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

#include <bits/stdc++.h>
#include <queue>
using namespace std;

#define what_is(x) cout << #x << " is " << x << endl;
#define PI acos(-1)
#define hell 1000000007
#define HELL 998244353
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fix(n) cout << fixed << setprecision(n)
#define mset(a, n) memset(a, n, sizeof a)
#define rep(i, a, b) for (__typeof((b)) i = (a); i < (b); i++)
#define repp(i, a, b, p) for (__typeof((b)) i = (a); i < (b); i += p)
#define ren(i, a, b) for (__typeof((a)) i = (a); i >= (b); i--)
#define renn(i, a, b, p) for(__typeof((a) i=(a);i>=(b);i-=p)
#define ADD(a, b, c) ((a) % c + (b) % c) % c
#define SUB(a, b, c) ((a) % c - (b) % c + c) % c
#define MUL(a, b, c) (((a) % c) * ((b) % c)) % c
#define lbd lower_bound
#define ubd upper_bound
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(v) (v).begin(), (v).end()
#define sz(x) (ll) x.size()
// #define out(n) cout<<n<<" "
#define outl(n) cout << n << endl
#define bug(n)                                                                 \\
  {                                                                            \\
    outl(n);                                                                   \\
    return;                                                                    \\
  }
#define pii pair<int, int>
#define lim 9223372036854775805
#define ninf -9223372036854775802
#define M 1000000007
#define eps 1e-9
#define MAX 10
#define N 500001
#define db(a)                                                                  \\
  for (auto it : a) {                                                          \\
    cout << it << " ";                                                         \\
  }                                                                            \\
  cout << endl;
ll prime[MAX];
ll fact[MAX];

const long long INF64 = 1e18;
ll vpow(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll temp = vpow(a, b / 2, p);
  temp *= temp;
  temp %= p;
  if (b % 2 == 0)
    return temp;
  temp = temp * a;
  temp %= p;
  return temp;
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll inverseMod(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll temp = vpow(b, p - 2, p);
  ll ans = temp * a;
  ans %= p;
  return ans;
}
void sieve() {
  prime[0] = prime[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    if (prime[i] == 0) {
      for (int j = i * 2; j < MAX; j += i) {
        if (prime[j] == 0) {
          prime[j] = i;
        }
      }
      prime[i] = i;
    }
  }
}
ll max(ll a, ll b) { return a > b ? a : b; }

void factorial() {
  fact[0] = 1;
  fact[1] = 1;
  rep(i, 2, MAX) {
    fact[i] = (fact[i - 1] * i);
    // fact[i]%=M;
  }
}
ll rand(ll a, ll b) { return a + rand() % (b - a + 1); }
unordered_map<ll, ll> dp;
int call(ll i, ll j) {
  if (dp.count(j))
    return dp[j];
  dp[j] = (((call(i, j - 3) * call(i, j - 4)) % M) * call(i, j - 5)) % M;
  dp[j] += 1;
  dp[j] %= M;
  return dp[j];
}

void solve(ll ttt) {
  ll n, i;
  cin >> n;
  vector<int> a(n, 0), b(n, 0);
  map<int, int> a1, b1;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    a1[a[i]]++;
  }

  for (i = 0; i < n; i++) {
    cin >> b[i];
    b1[b[i]]++;
  }
  auto x = a1.begin(), y = b1.begin();
  /*while(x!=a1.end())
  {
      cout<<(*x).first<<" "<<(*x).second<<endl;
      x++;
  }*/

  while (x != a1.end() && y != b1.end()) {
    if ((*x).first == (*y).first && (*x).second + (*y).second > n) {
      cout << "No" << endl;
      return;
    } else if ((*x).first > (*y).first)
      y++;
    else if ((*x).first < (*y).first)
      x++;
    else
      x++, y++;
  }
  // cout<<"abc"<<endl;
  ll j, le, ri, c;
  reverse(b.begin(), b.end());
  for (i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      le = i;
      c = a[i];
      for (j = i; j < n; j++) {
        if (a[j] == b[j])
          ri = j;
      }

      break;
    }
  }
  for (i = 0; i < n; i++) {
    if (le > ri)
      break;
    if (a[i] != c && b[i] != c)
      swap(b[i], b[le++]);
  }
  cout << "Yes" << endl;
  for (i = 0; i < n; i++)
    cout << b[i] << " ";
}

int main() {
  io;
  // sieve();
  // factorial();
#ifndef ONLINE_JUDGE
  //
  // freopen("input1.txt","r",stdin);
  // freopen("output1.txt","w",stdout);
#endif
  ll t = 1;
  // cin>>t;
  fix(12);
  rep(q, 1, t + 1) {
    // cout<<"Case #"<<q<<": ";
    // cout<<"Test Case : "<<q<<endl;
    solve(q);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
using namespace std;

#define what_is(x) cout << #x << " is " << x << endl;
#define PI acos(-1)
#define hell 1000000007
#define HELL 998244353
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fix(n) cout << fixed << setprecision(n)
#define mset(a, n) memset(a, n, sizeof a)
#define rep(i, a, b) for (__typeof((b)) i = (a); i < (b); i++)
#define repp(i, a, b, p) for (__typeof((b)) i = (a); i < (b); i += p)
#define ren(i, a, b) for (__typeof((a)) i = (a); i >= (b); i--)
#define renn(i, a, b, p) for(__typeof((a) i=(a);i>=(b);i-=p)
#define ADD(a, b, c) ((a) % c + (b) % c) % c
#define SUB(a, b, c) ((a) % c - (b) % c + c) % c
#define MUL(a, b, c) (((a) % c) * ((b) % c)) % c
#define lbd lower_bound
#define ubd upper_bound
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(v) (v).begin(), (v).end()
#define sz(x) (ll) x.size()
// #define out(n) cout<<n<<" "
#define outl(n) cout << n << endl
#define bug(n)                                                                 \\
  {                                                                            \\
    outl(n);                                                                   \\
    return;                                                                    \\
  }
#define pii pair<int, int>
#define lim 9223372036854775805
#define ninf -9223372036854775802
#define M 1000000007
#define eps 1e-9
#define MAX 10
#define N 500001
#define db(a)                                                                  \\
  for (auto it : a) {                                                          \\
    cout << it << " ";                                                         \\
  }                                                                            \\
  cout << endl;
ll prime[MAX];
ll fact[MAX];

const long long INF64 = 1e18;
ll vpow(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll temp = vpow(a, b / 2, p);
  temp *= temp;
  temp %= p;
  if (b % 2 == 0)
    return temp;
  temp = temp * a;
  temp %= p;
  return temp;
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll inverseMod(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll temp = vpow(b, p - 2, p);
  ll ans = temp * a;
  ans %= p;
  return ans;
}
void sieve() {
  prime[0] = prime[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    if (prime[i] == 0) {
      for (int j = i * 2; j < MAX; j += i) {
        if (prime[j] == 0) {
          prime[j] = i;
        }
      }
      prime[i] = i;
    }
  }
}
ll max(ll a, ll b) { return a > b ? a : b; }

void factorial() {
  fact[0] = 1;
  fact[1] = 1;
  rep(i, 2, MAX) {
    fact[i] = (fact[i - 1] * i);
    // fact[i]%=M;
  }
}
ll rand(ll a, ll b) { return a + rand() % (b - a + 1); }
unordered_map<ll, ll> dp;
int call(ll i, ll j) {
  if (dp.count(j))
    return dp[j];
  dp[j] = (((call(i, j - 3) * call(i, j - 4)) % M) * call(i, j - 5)) % M;
  dp[j] += 1;
  dp[j] %= M;
  return dp[j];
}

void solve(ll ttt) {
  ll n, i;
  cin >> n;
  vector<int> a(n, 0), b(n, 0);
  map<int, int> a1, b1;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    a1[a[i]]++;
  }

  for (i = 0; i < n; i++) {
    cin >> b[i];
    b1[b[i]]++;
  }
  auto x = a1.begin(), y = b1.begin();
  /*while(x!=a1.end())
  {
      cout<<(*x).first<<" "<<(*x).second<<endl;
      x++;
  }*/

  while (x != a1.end() && y != b1.end()) {
    if ((*x).first == (*y).first && (*x).second + (*y).second > n) {
      cout << "No" << endl;
      return;
    } else if ((*x).first > (*y).first)
      y++;
    else if ((*x).first < (*y).first)
      x++;
    else
      x++, y++;
  }
  // cout<<"abc"<<endl;
  ll j = 1;
  for (i = 0; j < n; j++) {
    if (a[i] == b[i] && a[j] == b[j] && a[i] != b[j]) {
      swap(b[i], b[j]);
      while (i < n && a[i] != b[i])
        i++;
      j = i;
    }
  }
  for (i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      j = (i + 1) % n;
      while (b[j] == a[j] || b[i] == a[j] || a[i] == b[j]) {
        j = (j + 1) % n;
      }
      swap(b[i], b[j]);
    }
  }
  cout << "Yes" << endl;
  for (i = 0; i < n; i++)
    cout << b[i] << " ";
  cout << endl;
  return;
}

int main() {
  io;
  // sieve();
  // factorial();
#ifndef ONLINE_JUDGE
  //
  // freopen("input1.txt","r",stdin);
  // freopen("output1.txt","w",stdout);
#endif
  ll t = 1;
  // cin>>t;
  fix(12);
  rep(q, 1, t + 1) {
    // cout<<"Case #"<<q<<": ";
    // cout<<"Test Case : "<<q<<endl;
    solve(q);
  }
  return 0;
}