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

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
#define ld long double
// #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>,
// rb_tree_tag, tree_order_statistics_node_update>
ll mod = 1e9 + 7;
#define PI 3.1415926535897932385
#define inf 9e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}
ll min(ll a, ll b) {
  if (a < b) {
    return a;
  }
  return b;
}
#define MAXN 200005
// Comment this out for interactive problem
// #define endl '\\n'
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output

int main() {
  fastio;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

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

  reverse(b, b + n);

  ll c = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] == b[i]) {
      c = b[i];
      break;
    }
  }

  if (c == -1) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; ++i) {
      cout << b[i] << " ";
    }
    cout << endl;
    return 0;
  }

  ll l = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] == c && b[i] == c) {
      l = i;
      break;
    }
  }

  ll r = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] == c && b[i] == c) {
      r = i;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (a[i] != c && b[i] != c && l <= r) {
      swap(b[i], b[l]);
      ++l;
    }
  }

  if (l <= r) {
    cout << "No";
    return 0;
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
#define ld long double
// #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>,
// rb_tree_tag, tree_order_statistics_node_update>
ll mod = 1e9 + 7;
#define PI 3.1415926535897932385
#define inf 9e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}
ll min(ll a, ll b) {
  if (a < b) {
    return a;
  }
  return b;
}
#define MAXN 200005
// Comment this out for interactive problem
// #define endl '\\n'
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output

int main() {
  fastio;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

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

  map<ll, ll> m2;
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    m2[b[i]]++;
  }

  ll fg = 0;
  vector<ii> v1;
  for (auto i = m1.begin(); i != m1.end(); ++i) {
    ll x1 = i->S + m2[i->F];
    if (x1 > n) {
      fg = 1;
    }
    v1.push_back({i->S, i->F});
  }
  sort(v1.rbegin(), v1.rend());

  vector<ii> v2;
  for (auto i = m2.begin(); i != m2.end(); ++i) {
    v2.push_back({i->S, i->F});
  }
  sort(v2.rbegin(), v2.rend());

  if (fg == 1) {
    cout << "No";
    return 0;
  }

  deque<ll> q1;
  for (int i = 0; i < v2.size(); ++i) {
    for (int j = 0; j < v2[i].F; ++j) {
      q1.push_back(v2[i].S);
    }
  }

  vector<ll> ans;
  ll ind = 0;
  while (!q1.empty()) {
    if (q1.front() == a[ind]) {
      ll x1 = q1.front();
      q1.push_back(x1);
    } else {
      ind++;
      ans.push_back(q1.front());
    }
    q1.pop_front();
  }

  cout << "Yes" << endl;
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << " ";
  }
  cout << endl;

  return 0;
}