Submission #1213053

#TimeUsernameProblemLanguageResultExecution timeMemory
1213053LemserSorting (IOI15_sorting)C++20
0 / 100
1 ms328 KiB
#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];
	if (is_sorted(all(a))) return 0ll;
	make_swap(0, 1, a, pos);
	ll R = 0;
	fo (i, n) pos[a[i]] = i;
	if (pos[0] > 1) {
		P[R] = 0;
		Q[R] = pos[0];
		R++;
		make_swap(0, pos[0], a, pos);
		if (is_sorted(all(a))) return R;
		make_swap(0, 1, a, pos);
	}
	if (pos[1] > 1) {
		ll f = (a[1] == 0 ? 0 : 1);
		P[R] = f;
		Q[R] = pos[1];
		R++;
		make_swap(f, pos[1], a, pos);
		if (is_sorted(all(a))) return R;
		make_swap(0, 1, a, pos);
	}
	assert(pos[0] <= 1);
	assert(pos[1] <= 1);
	assert(a[0]+a[1]==1);
	while (!is_sorted(a.begin()+2, a.end())) {
		fore (i, 2, n) {
			if (a[i] != i) {
				P[R] = i;
				Q[R] = a[i];
				make_swap(i, a[i], a, pos);
				R++;
				if (is_sorted(all(a))) return R;
				make_swap(0, 1, a, pos);
				break;
			}
		}
	}
	if (!is_sorted(all(a))) {
		P[R] = 0;
		Q[R] = 1;
		make_swap(0, 1, a, pos);
		R++;
	}
	assert(is_sorted(all(a)));
	return R;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...