Submission #32168

# Submission time Handle Problem Language Result Execution time Memory
32168 2017-10-01T13:57:30 Z imeimi2000 Sorting (IOI15_sorting) C++14
100 / 100
226 ms 14900 KB
#include "sorting.h"
#include <vector>
#include <algorithm>

using namespace std;
typedef long long llong;

int arr[200000];
int x[200000];
int y[200000];
int n, m;
int tmp[200000];
bool ch[200000];
int ps[200000];
int qs[200000];

bool check(int r) {
	int j, cnt = 0;
	for (int i = 0; i < n; ++i) tmp[i] = arr[i], ch[i] = false;
	for (int i = 0; i < r; ++i) swap(tmp[x[i]], tmp[y[i]]);
	for (int i = 0; i < n; ++i) {
		if (ch[i]) continue;
		ch[i] = true;
		j = tmp[i];
		while (j != i) ++cnt, ch[j] = true, j = tmp[j];
	}
	return cnt <= r;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n = m = N;
	for (int i = 0; i < n; ++i) arr[i] = S[i];
	for (int i = 0; i < m; ++i) x[i] = X[i], y[i] = Y[i];
	int s = 0, e = m;
	while (s < e) {
		int t = (s + e) / 2;
		if (check(t)) e = t;
		else s = t + 1;
	}
	for (int i = 0; i < n; ++i) tmp[i] = arr[i];
	for (int i = 0; i < s; ++i) {
		swap(tmp[x[i]], tmp[y[i]]);
	}
	for (int i = 0, j = 0; i < m; ++i) {
		while (j < n && tmp[j] == j) ++j;
		if (j == n) {
			for (; i < m; ++i) ps[i] = qs[i] = 0;
		}
		ps[i] = tmp[j]; qs[i] = tmp[tmp[j]];
		swap(tmp[j], tmp[tmp[j]]);
	}
	for (int i = 0; i < n; ++i) tmp[arr[i]] = i;
	for (int i = 0; i < s; ++i) {
		int p = ps[i], q = qs[i];
		swap(tmp[arr[x[i]]], tmp[arr[y[i]]]);
		swap(arr[x[i]], arr[y[i]]);
		P[i] = tmp[p];
		Q[i] = tmp[q];
		swap(arr[tmp[p]], arr[tmp[q]]);
		swap(tmp[p], tmp[q]);
	}
	return s;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:30:39: warning: unused parameter 'M' [-Wunused-parameter]
 int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
                                       ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 356 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 356 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 356 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 356 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 3 ms 356 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 484 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 157 ms 12336 KB Output is correct
16 Correct 173 ms 12660 KB Output is correct
17 Correct 178 ms 13688 KB Output is correct
18 Correct 54 ms 9824 KB Output is correct
19 Correct 136 ms 12408 KB Output is correct
20 Correct 168 ms 12796 KB Output is correct
21 Correct 172 ms 12928 KB Output is correct
22 Correct 140 ms 13596 KB Output is correct
23 Correct 211 ms 13816 KB Output is correct
24 Correct 221 ms 13768 KB Output is correct
25 Correct 226 ms 14072 KB Output is correct
26 Correct 135 ms 12664 KB Output is correct
27 Correct 121 ms 12152 KB Output is correct
28 Correct 178 ms 13560 KB Output is correct
29 Correct 192 ms 13688 KB Output is correct
30 Correct 89 ms 11640 KB Output is correct
31 Correct 199 ms 13944 KB Output is correct
32 Correct 168 ms 13364 KB Output is correct
33 Correct 222 ms 14072 KB Output is correct
34 Correct 177 ms 13820 KB Output is correct
35 Correct 136 ms 12280 KB Output is correct
36 Correct 54 ms 10744 KB Output is correct
37 Correct 225 ms 14900 KB Output is correct
38 Correct 171 ms 13916 KB Output is correct
39 Correct 199 ms 14368 KB Output is correct