답안 #202814

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
202814 2020-02-18T01:07:10 Z spdskatr 정렬하기 (IOI15_sorting) C++14
100 / 100
230 ms 33528 KB
#include "sorting.h"
#include <cstring>
#include <algorithm>
using namespace std;

int N, M, s[2000005], x[2000005], y[2000005], state[2000005], seen[2000005], pos[2000005];

int check(int steps) {
	for (int i = 0; i < N; i++) state[i] = s[i];
	for (int i = 0; i < steps; i++) {
		if (x[i] != y[i]) {
			swap(state[x[i]], state[y[i]]);
		}
	}
	int cycles = 0;
	for (int i = 0; i < N; i++) {
		if (!seen[i]) {
			seen[i] = 1;
			cycles++;
			int cur = state[i];
			while (cur != i) {
				seen[cur] = 1;
				cur = state[cur];
			}
		}
	}
	int swaps = N - cycles;
	memset(seen, 0, sizeof(seen));
	return swaps <= steps;
}

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++) s[i] = S[i];
	for (int i = 0; i < M; i++) {
		x[i] = X[i];
		y[i] = Y[i];
	}
	int lo = -1, hi = M;
	while (lo + 1 < hi) {
		int mid = (lo + hi) / 2;
		if (check(mid)) hi = mid;
		else lo = mid;
	}
	// Takes hi number of swaps
	for (int i = 0; i < N; i++) state[i] = s[i];
	for (int i = 0; i < hi; i++) {
		if (x[i] != y[i]) {
			swap(state[x[i]], state[y[i]]);
		}
	}
	int R = 0;
	for (int i = 0; i < N; i++) {
		if (!seen[i]) {
			seen[i] = 1;
			if (state[i] != i) {
				int cur = state[i];
				while (cur != i) {
					// Swap cur with state[cur]
					seen[cur] = 1;
					cur = state[cur];
					P[R] = cur;
					Q[R] = state[cur];
					R++;
				}
			}
		}
	}
	for (int i = 0; i < N; i++) pos[s[i]] = i, state[i] = s[i];
	for (int i = 0; i < R; i++) {
		swap(pos[state[x[i]]], pos[state[y[i]]]);
		swap(state[x[i]], state[y[i]]);
		swap(pos[P[i]], pos[Q[i]]);
		P[i] = pos[P[i]];
		Q[i] = pos[Q[i]];
		swap(state[P[i]], state[Q[i]]);
	}
	for (int i = R; i < hi; i++) P[i] = Q[i] = 0;
	return hi;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8184 KB Output is correct
2 Correct 9 ms 8184 KB Output is correct
3 Correct 10 ms 8184 KB Output is correct
4 Correct 10 ms 8184 KB Output is correct
5 Correct 11 ms 8184 KB Output is correct
6 Correct 12 ms 8184 KB Output is correct
7 Correct 11 ms 8184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8184 KB Output is correct
2 Correct 9 ms 8184 KB Output is correct
3 Correct 10 ms 8184 KB Output is correct
4 Correct 10 ms 8184 KB Output is correct
5 Correct 11 ms 8184 KB Output is correct
6 Correct 12 ms 8184 KB Output is correct
7 Correct 11 ms 8184 KB Output is correct
8 Correct 11 ms 8184 KB Output is correct
9 Correct 13 ms 8184 KB Output is correct
10 Correct 16 ms 8184 KB Output is correct
11 Correct 16 ms 8184 KB Output is correct
12 Correct 16 ms 8184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8184 KB Output is correct
2 Correct 13 ms 8184 KB Output is correct
3 Correct 17 ms 8184 KB Output is correct
4 Correct 16 ms 8184 KB Output is correct
5 Correct 17 ms 8188 KB Output is correct
6 Correct 14 ms 8184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8184 KB Output is correct
2 Correct 9 ms 8184 KB Output is correct
3 Correct 10 ms 8184 KB Output is correct
4 Correct 10 ms 8184 KB Output is correct
5 Correct 11 ms 8184 KB Output is correct
6 Correct 12 ms 8184 KB Output is correct
7 Correct 11 ms 8184 KB Output is correct
8 Correct 11 ms 8184 KB Output is correct
9 Correct 13 ms 8184 KB Output is correct
10 Correct 16 ms 8184 KB Output is correct
11 Correct 16 ms 8184 KB Output is correct
12 Correct 16 ms 8184 KB Output is correct
13 Correct 11 ms 8184 KB Output is correct
14 Correct 13 ms 8184 KB Output is correct
15 Correct 17 ms 8184 KB Output is correct
16 Correct 16 ms 8184 KB Output is correct
17 Correct 17 ms 8188 KB Output is correct
18 Correct 14 ms 8184 KB Output is correct
19 Correct 11 ms 8184 KB Output is correct
20 Correct 12 ms 8184 KB Output is correct
21 Correct 21 ms 8440 KB Output is correct
22 Correct 18 ms 8444 KB Output is correct
23 Correct 19 ms 8440 KB Output is correct
24 Correct 20 ms 8420 KB Output is correct
25 Correct 18 ms 8440 KB Output is correct
26 Correct 23 ms 8568 KB Output is correct
27 Correct 21 ms 8440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 8312 KB Output is correct
2 Correct 18 ms 8312 KB Output is correct
3 Correct 19 ms 8312 KB Output is correct
4 Correct 18 ms 8312 KB Output is correct
5 Correct 18 ms 8312 KB Output is correct
6 Correct 18 ms 8312 KB Output is correct
7 Correct 19 ms 8312 KB Output is correct
8 Correct 18 ms 8312 KB Output is correct
9 Correct 17 ms 8312 KB Output is correct
10 Correct 18 ms 8312 KB Output is correct
11 Correct 18 ms 8312 KB Output is correct
12 Correct 19 ms 8312 KB Output is correct
13 Correct 17 ms 8312 KB Output is correct
14 Correct 18 ms 8312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 8312 KB Output is correct
2 Correct 18 ms 8312 KB Output is correct
3 Correct 19 ms 8312 KB Output is correct
4 Correct 18 ms 8312 KB Output is correct
5 Correct 18 ms 8312 KB Output is correct
6 Correct 18 ms 8312 KB Output is correct
7 Correct 19 ms 8312 KB Output is correct
8 Correct 18 ms 8312 KB Output is correct
9 Correct 17 ms 8312 KB Output is correct
10 Correct 18 ms 8312 KB Output is correct
11 Correct 18 ms 8312 KB Output is correct
12 Correct 19 ms 8312 KB Output is correct
13 Correct 17 ms 8312 KB Output is correct
14 Correct 18 ms 8312 KB Output is correct
15 Correct 179 ms 23032 KB Output is correct
16 Correct 190 ms 23288 KB Output is correct
17 Correct 205 ms 24056 KB Output is correct
18 Correct 90 ms 19832 KB Output is correct
19 Correct 164 ms 22264 KB Output is correct
20 Correct 183 ms 22920 KB Output is correct
21 Correct 175 ms 22904 KB Output is correct
22 Correct 190 ms 24312 KB Output is correct
23 Correct 197 ms 24696 KB Output is correct
24 Correct 210 ms 24312 KB Output is correct
25 Correct 207 ms 24184 KB Output is correct
26 Correct 180 ms 27768 KB Output is correct
27 Correct 165 ms 30844 KB Output is correct
28 Correct 213 ms 32892 KB Output is correct
29 Correct 205 ms 32248 KB Output is correct
30 Correct 131 ms 30204 KB Output is correct
31 Correct 206 ms 32760 KB Output is correct
32 Correct 212 ms 32380 KB Output is correct
33 Correct 229 ms 33144 KB Output is correct
34 Correct 222 ms 32888 KB Output is correct
35 Correct 183 ms 30712 KB Output is correct
36 Correct 89 ms 29176 KB Output is correct
37 Correct 230 ms 33528 KB Output is correct
38 Correct 221 ms 32632 KB Output is correct
39 Correct 222 ms 32888 KB Output is correct