Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- #define endl '\n'
- const int N = 2e3 + 30;
- int n;
- int a[N], b[N], pos[N];
- void solve() {
- cin >> n;
- for (int i = 1; i <= n; i ++) {
- cin >> a[i];
- }
- for (int i = 1; i <= n; i ++) {
- cin >> b[i];
- pos[b[i]] = i;
- }
- vector<pair<int, int>> ans;
- for (int now = n; now >= 1; now --) {
- int l = 1, r = 1;
- while (a[l] != now) l ++;
- r = pos[a[l]];
- // cout << now << ' ' << l << ' ' << r << endl;
- // for (int i = 1; i <= n; i ++) cout << a[i] << " \n"[i == n];
- if (l == r) continue;
- if (l > r || a[r] > a[l]) {
- // cout << a[l] << ' ' << a[r] << endl;
- cout << -1 << endl;
- return;
- }
- deque<int> st;
- for (int i = l + 1; i <= r; i ++) {
- if (a[i] > now) continue;
- if (!st.empty() && a[st.back()] < a[i]) st.pop_back();
- st.push_back(i);
- }
- int lst = l;
- for (auto x: st) {
- swap(a[lst], a[x]);
- lst = x;
- ans.push_back({l, x});
- }
- }
- for (int i = 1; i <= n; i ++) {
- assert(a[i] == b[i]);
- }
- cout << ans.size() << endl;
- for (auto [l, r]: ans) {
- cout << l << ' ' << r << endl;
- }
- }
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(0), cout.tie(0);
- int _ = 1;
- cin >> _;
- while (_--){
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement