Language: C++

/* 	* In the name of GOD 
 * Thanks God */
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define F first
#define S second
const int N = 201234;
int a[N], b[N], ans[N];
vector <int> ha[N], hb[N], ha2[N], hb2[N];

void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i], ha[a[i]].push_back(i), ha2[a[i]].push_back(i);
	for (int i = 0; i < n; i++)
		cin >> b[i], hb[b[i]].push_back(i), hb2[b[i]].push_back(i);
	if (n <= 10) {
		vector <int> x;
		for (int i = 1; i <= n; i++) {
			x.push_back(i);
		}
		do {
			bool f = true;
			for (int i = 0; i < n; i++) {
				if (b[x[i] - 1] == a[i]) {
					f = false;
					break;
				}
			}
			if (f) {
				cout << "Yes\n";
				for (int i = 0; i < n; i++)
					cout << b[x[i] - 1] << ' ';
				return;
			}
		} while (next_permutation(x.begin(), x.end()));
		cout << "No";
		return;
	}
	set <int> st;
	for (int i = 0; i < n; i++)
		st.insert(i);
	for (int i = 0; i < N; i++) {
		vector <int> all;
		for (auto x : ha[i]) {
			if (st.count(x))
				all.push_back(x);
			st.erase(x);
		}
		vector <int> v = hb[i];
		while (st.size() && v.size()) {
			int in = *st.rbegin();
			ans[in] = i;
			st.erase(in);
			v.pop_back();
		}
		if (v.size()) {
			goto ed;
		}
		for (auto x : all)
			st.insert(x);
	}
	cout << "Yes\n";
	for (int i = 0; i < n; i++)
		cout << ans[i] << ' ';
	return;
	ed:
	st.clear();
	for (int i = 0; i < n; i++)
		st.insert(i);
	for (int i = 0; i < N; i++) 
		ans[i] = 0, ha[i] = ha2[i], hb[i] = hb2[i];
	for (int i = 0; i < N; i++) {
		vector <int> all;
		for (auto x : ha[i]) {
			if (st.count(x))
				all.push_back(x);
			st.erase(x);
		}
		vector <int> v = hb[i];
		while (st.size() && v.size()) {
			int in = *st.begin();
			ans[in] = i;
			st.erase(in);
			v.pop_back();
		}
		if (v.size()) {
			goto ed2;
		}
		for (auto x : all)
			st.insert(x);
	}
	cout << "Yes\n";
	for (int i = 0; i < n; i++)
		cout << ans[i] << ' ';
	return;
	ed2:
	unordered_set <int> st2;
	st2.clear();
	for (int i = 0; i < n; i++)
		st2.insert(i);
	for (int i = 0; i < N; i++) 
		ans[i] = 0, ha[i] = ha2[i], hb[i] = hb2[i];
	for (int i = 0; i < N; i++) {
		vector <int> all;
		for (auto x : ha[i]) {
			if (st.count(x))
				all.push_back(x);
			st2.erase(x);
		}
		vector <int> v = hb[i];
		while (st.size() && v.size()) {
			int in = *st2.begin();
			ans[in] = i;
			st2.erase(in);
			v.pop_back();
		}
		if (v.size()) {
			goto ed3;
		}
		for (auto x : all)
			st2.insert(x);
	}
	cout << "Yes\n";
	for (int i = 0; i < n; i++)
		cout << ans[i] << ' ';
	return;
	ed3:
	st2.clear();
	for (int i = 0; i < n; i++)
		st2.insert(i);
	for (int i = 0; i < N; i++) 
		ans[i] = 0, ha[i] = ha2[i], hb[i] = hb2[i];
	for (int i = 0; i < N; i++) {
		vector <int> all;
		for (auto x : ha[i]) {
			if (st.count(x))
				all.push_back(x);
			st2.erase(x);
		}
		vector <int> v = hb[i];
		while (st.size() && v.size()) {
			int in = *st2.rbegin();
			ans[in] = i;
			st2.erase(in);
			v.pop_back();
		}
		if (v.size()) {
			goto ed3;
		}
		for (auto x : all)
			st2.insert(x);
	}
	cout << "Yes\n";
	for (int i = 0; i < n; i++)
		cout << ans[i] << ' ';
	return;
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
}



Compiler output:
a.cc: In function 'void solve()':
a.cc:148:39: error: 'class std::unordered_set<int>' has no member named 'rbegin'; did you mean 'begin'?
  148 |                         int in = *st2.rbegin();
      |                                       ^~~~~~
      |                                       begin