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



// BELIEVE IN YOURSELF
// next_permutation

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define vi vector<ll>
#define mp make_pair
#define sz size()
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define maxx 100005
#define se second
#define fi first
#define be begin()
#define en end()
#define sortv(v) sort(v.begin(), v.end());
#define sortr(v) sort(v.rbegin(), v.rend());
#define w()                                                                    \\
  ll _;                                                                        \\
  cin >> _;                                                                    \\
  while (_--)
#define edge pair<ll, ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define Endl "\\n"
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
#define lb(v, x) std::lower_bound(v.begin(), v.end(), x)
#define ub(v, x) std::upper_bound(v.begin(), v.end(), x)

using namespace std;

int main() {
  fast;

  ll n;
  cin >> n;
  ll a[n];
  ll b[n];
  ll i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    cin >> b[i];
  }

  ll r = n - 1;

  for (i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      if (b[i] == b[r]) {
        r = n - 1;
      }
      swap(b[i], b[r]);
      r--;
    }
  }

  ll l = 0;

  for (i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      if (b[i] == b[l])
        l = 0;
      swap(b[i], b[l]);
      l++;
    }
  }
  ll fuck = 0;
  for (i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      fuck++;
    }
  }
  if (fuck) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (i = 0; i < n; i++) {
      cout << b[i] << " ";
    }
  }

  return 0;
}

Error: TLE

Fixed submission:



// BELIEVE IN YOURSELF
// next_permutation

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define vi vector<ll>
#define mp make_pair
#define sz size()
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define maxx 100005
#define se second
#define fi first
#define be begin()
#define en end()
#define sortv(v) sort(v.begin(), v.end());
#define sortr(v) sort(v.rbegin(), v.rend());
#define w()                                                                    \\
  ll _;                                                                        \\
  cin >> _;                                                                    \\
  while (_--)
#define edge pair<ll, ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define Endl "\\n"
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
#define lb(v, x) std::lower_bound(v.begin(), v.end(), x)
#define ub(v, x) std::upper_bound(v.begin(), v.end(), x)

using namespace std;

int main() {
  fast;

  ll n;
  cin >> n;
  ll a[n];
  ll b[n];
  ll i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  priority_queue<ll> q;
  for (i = 0; i < n; i++) {
    cin >> b[i];
    q.push(b[i]);
  }

  for (i = 0; i < n; i++) {
    if (a[i] != q.top()) {
      b[i] = q.top();
      q.pop();
    } else {
      stack<ll> s;
      while (a[i] == q.top() && !q.empty()) {
        s.push(q.top());
        q.pop();
      }
      if (q.empty()) {
        cout << "No" << endl;
        return 0;
      } else {
        b[i] = q.top();
        q.pop();
        while (!s.empty()) {
          q.push(s.top());
          s.pop();
        }
      }
    }
  }

  cout << "Yes" << endl;
  for (i = 0; i < n; i++) {
    cout << b[i] << " ";
  }

  return 0;
}