Submission #428294

#TimeUsernameProblemLanguageResultExecution timeMemory
428294haxormanSorting (IOI15_sorting)C++14
36 / 100
1080 ms516 KiB
#include "sorting.h"
#include "bits/stdc++.h"
using namespace std;

const int mxN = 2e5 + 7;

int n, m, arr[mxN], freq[mxN];

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n = N, m = M;

	for (int i = 0; i < n; ++i) {
		arr[i] = S[i];
	}

	for (int i = 0; i < m; ++i) {
		freq[X[i]]++;
		freq[Y[i]]++;
	}

	int R = 0;
	bool ok = false;
	for (int ind = 0; ind < m; ++ind) {
		ok = true;
		for (int i = 0; i < n; ++i) {
			if (arr[i] != i) {
				ok = false;
			}
		}

		if (ok) {
			break;
		}
		
		swap(arr[X[ind]], arr[Y[ind]]);
		freq[X[ind]]--;
		freq[Y[ind]]--;

		ok = true;
		for (int i = 0; i < n; ++i) {
			if (arr[i] != i) {
				ok = false;
			}
		}

		if (ok) {
			P[ind] = Q[ind] = 0;
			++R;
			break;
		}

		bool stop = false;
		for (int i = 0; i < n && !stop; ++i) {
			if (freq[i])
				continue;

			for (int j = 0; j < n && !stop; ++j) {
				if (arr[j] == i && i != j) {
					swap(arr[j], arr[i]);
					P[ind] = j, Q[ind] = i;
					++R;
					stop = true;
				}
			}
		}

		if (!stop) {
			P[ind] = Q[ind] = 0;
			++R;
		}

	}

	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...