Submission #426103

# Submission time Handle Problem Language Result Execution time Memory
426103 2021-06-13T14:06:49 Z _fractal Sorting (IOI15_sorting) C++14
0 / 100
463 ms 676 KB
#include "sorting.h"
#include <iostream>
#include <algorithm>
using namespace std;
 

int findSwapPairs(int N, int SS[], int M, int X[], int Y[], int P[], int Q[]) {
	int A[N], pos[N], L[M], R[M], ret = (1<<30), S[N];
	int tl = 0, tr = M - 1;
	while (tl <= tr) {
		int m = tl + tr >> 1;
		for (int i = 0; i < N; ++i)
			A[i] = i, S[i] = SS[i];
		for (int i = m; i >= 0; --i)
			swap(A[X[i]], A[Y[i]]);
		int bad = 0;
		for (int i = 0; i < N; ++i)
			if (S[i] != i)
				bad++;
		int r = -1;
		for (int i = 0; i <= m; ++i) {
			P[i] = Q[i] = 0;
			if (bad == 0) {
				r = i;
				break;
			}
			if (S[X[i]] != X[i])
				bad--;
			if (S[Y[i]] != Y[i])
				bad--;
			swap(S[X[i]], S[Y[i]]);
			swap(A[X[i]], A[Y[i]]);
			if (S[X[i]] != X[i])
				bad++;
			if (S[Y[i]] != Y[i])
				bad++;
			if (bad == 0) {
				r = i + 1;
				break;
			}
			for (int j = 0; j < N; ++j)
				pos[A[j]] = j;
			for (int j = 0; j < N; ++j) {
				if (pos[S[j]] != j) {
					Q[i] = j, P[i] = pos[S[j]];
					break;
				}
			}
			if (S[Q[i]] != Q[i])
				bad--;
			if (S[P[i]] != P[i])
				bad--;
			swap(S[Q[i]], S[P[i]]);
			if (S[Q[i]] != Q[i])
				bad++;
			if (S[P[i]] != P[i])
				bad++;
		}
		if (!bad) {
			tl = m + 1;
			continue;
		}
		tr = m - 1;
		if (r == -1) r = m + 1;
		if (ret >= r) {
			ret = r;
			for (int i = 0; i < r; ++i)
				L[i] = Q[i], R[i] = P[i];
		}
	}
	for (int i = 0; i < ret; ++i)
		Q[i] = L[i], P[i]= R[i];
	return ret;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:11:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 |   int m = tl + tr >> 1;
      |           ~~~^~~~
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 7
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 463 ms 676 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 463 ms 676 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -