Submission #225876

#TimeUsernameProblemLanguageResultExecution timeMemory
225876staniewzkiSorting (IOI15_sorting)C++17
100 / 100
393 ms20304 KiB
#include<bits/stdc++.h>
using namespace std;
 
#define REP(i, n) for(int i = 0; i < n; i++)
 
#include "sorting.h"

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	auto check = [&](int R) {
		vector<int> s(N), where(N), where_f(N);
		REP(i, N) s[i] = S[i];
		auto f = s;
		REP(i, R) swap(f[X[i]], f[Y[i]]);
		REP(i, N) {
			where[S[i]] = i;
			where_f[f[i]] = i;
		}

		int j = 0;
		REP(i, R) P[i] = Q[i] = 0;
		REP(i, R) {
			int &p = s[X[i]], &q = s[Y[i]];
			swap(p, q);
			swap(where[p], where[q]);

			while(j < N) {
				int &a = where[j], &x = where_f[j];
				int &b = where[f[j]], &y = where_f[f[j]];
				j++;
				if(a == b) continue;
				P[i] = a, Q[i] = b;
				swap(a, b), swap(s[a], s[b]);
				swap(x, y), swap(f[x], f[y]);
				break;
			}
		}
		REP(i, N) if(i != s[i]) return false;
		return true;
	};

	int l = 0, r = M;
	while(l < r) {
		int m = (l + r) / 2;
		if(check(m))
			r = m;
		else
			l = m + 1;
	}

	check(l);
	return l;
}
#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...