#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using lld = long double;
using ii = pair<int,int>;
using pll = pair<ll, ll>;
 
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
using vpll = vector<pll>;
using vlld = vector<lld>;
 
#define all(x) x.begin(),x.end()
#define lsb(x) x&(-x)
#define gcd(a,b) __gcd(a,b)
#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fls cout.flush()
 
#define fore(i, l, r) for (auto i = l; i < r; i++)
#define fo(i, n) fore (i, 0, n)
#define forex(i, r, l) for (auto i = r-1; i >= l; i--)
#define ffo(i, n) forex (i, n, 0)
 
bool cmin(ll &a, ll b) { if (b < a) { a=b; return 1; } return 0; }
bool cmax(ll &a, ll b) { if (b > a) { a=b; return 1; } return 0; }
void make_swap (ll i, ll j, vector<int> &a, vector<int> &pos) {
	swap(a[i], a[j]);
	swap(pos[a[i]], pos[a[j]]);
	// for (ll i: a) cout << i << ' ';
	// cout << '\n';
}
int findSwapPairs(int n, int A[], int M, int X[], int Y[], int P[], int Q[]) {
	vector<int> a(n), pos(n);
	fo (i, n) a[i] = A[i];
	fo (i, n) pos[a[i]] = i;
	if (is_sorted(all(a))) return 0ll;
	auto b = a, posb = pos;
	fo (i, M) make_swap(X[i], Y[i], b, posb);
	ll R = 0;
	make_swap (posb[a[X[0]]], posb[a[Y[0]]], b, posb);
	make_swap (X[0], Y[0], a, pos);
	while (R < M) {
		if (is_sorted(all(b))) {
			P[R] = Q[R] = 0;
			R++;
			if (is_sorted(all(a))) return R;
			if (R < M) {
				make_swap (posb[a[X[R]]], posb[a[Y[R]]], b, posb);
				make_swap (X[R], Y[R], a, pos);
			}
			continue;
		}
		fo (i, n) {
			if (b[i] == i) continue;
			ll pos_a = pos[b[i]], pos_b = pos[i];
			P[R] = pos_a;
			Q[R] = pos_b;
			make_swap (posb[a[pos_a]], posb[a[pos_b]], b, posb);
			make_swap (pos_a, pos_b, a, pos);
			break;
		}
		R++;
		if (is_sorted(all(a))) return R;
		if (R < M) {
			make_swap (posb[a[X[R]]], posb[a[Y[R]]], b, posb);
			make_swap (X[R], Y[R], a, pos);
		}
	}
	return R;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |