답안 #288397

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
288397 2020-09-01T13:13:43 Z amoo_safar 정렬하기 (IOI15_sorting) C++17
100 / 100
815 ms 19192 KB
#include "sorting.h"

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()

using namespace std;

int findSwapPairs(int n, int seq[], int m, int X[], int Y[], int P[], int Q[]) {
    P[0] = 0;
	Q[0] = 0;

	vector<int> per(n), idx(n), mk(n);
	
	int L = -1, R = m, mid;
	int d;
	while(L + 1 < R){
		mid = (L + R) >> 1;
		iota(all(per), 0);
		fill(all(mk), 0);
		d = 0;
		
		for(int i = mid - 1; i >= 0; i--){
			d ++;
			if(X[i] != Y[i])
				swap(per[X[i]], per[Y[i]]);
		}

		for(int i = 0; i < n; i++) idx[per[i]] = i;

		int cnt = 0, nw;
		for(int i = 0; i < n; i++){
			if(mk[seq[i]]) continue;
			cnt ++;
			nw = seq[i];
			do {
				mk[nw] = 1;
				nw = seq[idx[nw]];
			} while(!mk[nw]);
		}

		if(n - cnt <= d) R = mid;
		else L = mid;
	}
	cerr << "! " << R << '\n';


	iota(all(per), 0);
	fill(all(mk), 0);	
	for(int i = R - 1; i >= 0; i--){
		if(X[i] != Y[i])
			swap(per[X[i]], per[Y[i]]);
	}
	
	for(int i = 0; i < n; i++) idx[per[i]] = i;

	set<int> nq;
	for(int i = 0; i < n; i++) if(seq[i] != per[i]) nq.insert(i);

	for(int i = 0; i < R; i++){
		if(X[i] != Y[i]){
			nq.erase(X[i]);
			nq.erase(Y[i]);
			
			swap(seq[X[i]], seq[Y[i]]);
			swap(per[X[i]], per[Y[i]]);

			idx[per[X[i]]] = X[i];
			idx[per[Y[i]]] = Y[i];
		
			if(seq[X[i]] != per[X[i]])
				nq.insert(X[i]);
			if(seq[Y[i]] != per[Y[i]])
				nq.insert(Y[i]);
		}

		if(!nq.empty()){
			int fr = *nq.begin();
			P[i] = fr;
			Q[i] = idx[seq[fr]];
			assert(P[i] != Q[i]);

			nq.erase(P[i]);
			nq.erase(Q[i]);
			
			swap(seq[P[i]], seq[Q[i]]);
		
			if(seq[P[i]] != per[P[i]])
				nq.insert(P[i]);
			if(seq[Q[i]] != per[Q[i]])
				nq.insert(Q[i]);
		}

	}

	for(int i = 0; i < n; i++) assert(seq[i] == i);
	return R;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 0 ms 288 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 0 ms 288 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 1 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 564 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 544 KB Output is correct
8 Correct 3 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 2 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 564 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 544 KB Output is correct
8 Correct 3 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 2 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 486 ms 17144 KB Output is correct
16 Correct 582 ms 17400 KB Output is correct
17 Correct 759 ms 18296 KB Output is correct
18 Correct 92 ms 7548 KB Output is correct
19 Correct 381 ms 14200 KB Output is correct
20 Correct 444 ms 14968 KB Output is correct
21 Correct 449 ms 14968 KB Output is correct
22 Correct 491 ms 18448 KB Output is correct
23 Correct 570 ms 18868 KB Output is correct
24 Correct 781 ms 18552 KB Output is correct
25 Correct 770 ms 18400 KB Output is correct
26 Correct 403 ms 14328 KB Output is correct
27 Correct 378 ms 13068 KB Output is correct
28 Correct 738 ms 18424 KB Output is correct
29 Correct 688 ms 18168 KB Output is correct
30 Correct 213 ms 13048 KB Output is correct
31 Correct 711 ms 18744 KB Output is correct
32 Correct 566 ms 18260 KB Output is correct
33 Correct 782 ms 18612 KB Output is correct
34 Correct 649 ms 18552 KB Output is correct
35 Correct 413 ms 14328 KB Output is correct
36 Correct 71 ms 8056 KB Output is correct
37 Correct 815 ms 19192 KB Output is correct
38 Correct 779 ms 18296 KB Output is correct
39 Correct 799 ms 18552 KB Output is correct