답안 #583861

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
583861 2022-06-26T10:33:26 Z M_W 정렬하기 (IOI15_sorting) C++17
36 / 100
13 ms 468 KB
#include <bits/stdc++.h>
#define ii pair<int, int>
using namespace std;
int S[200002];
int gp = 0, pa[200002], from[200002], aa[200002];
bool vis[200002];

int findpa(int a){
	return pa[a] == a ? a : findpa(pa[a]);
}

int findSwapPairs(int N, int S_tmp[], int M, int X[], int Y[], int P[], int Q[]) {
    for(int i = 0; i < N; i++){
    	S[i] = S_tmp[i]; pa[i] = -1;
	}
    for(int i = 0; i < N; i++){
    	if(vis[i]) continue;
    	int cur = i;
    	while(!vis[cur]){
    		vis[cur] = true;
    		cur = S[cur];
		}
		gp++;
	}
	
	// Already sorted
	if(N == gp){
		P[0] = 0; Q[0] = 0;
		return 0;
	}
	// Nah
	for(int k = 0; k < N; k++){
		swap(S[X[k]], S[Y[k]]);
		
		gp = 0;
		for(int i = 0; i < N; i++) vis[i] = false;
		for(int i = 0; i < N; i++){
	    	if(vis[i]) continue;
	    	int cur = i;
	    	while(!vis[cur]){
	    		vis[cur] = true;
	    		from[S[cur]] = cur;
	    		cur = S[cur];
			}
			gp++;
		}
		
		if(N - k - 1 <= gp){
			queue<ii> q;
			for(int i = 0; i < N; i++) vis[i] = false;
			for(int i = 0; i < N; i++){
				if(vis[i]) continue;
				int cur = from[i]; vis[i] = true;
				while(!vis[cur]){
					vis[cur] = true;
					q.push({i, cur});
					cur = from[cur];
				}
			}
			
			for(int i = 0; i < N; i++) aa[i] = i;
			for(int j = k; j >= 0; j--){
				if(q.empty()) P[j] = Q[j] = 0;
				else{
					auto [u, v] = q.front();
					q.pop();
					
					P[j] = aa[u];
					Q[j] = aa[v];
					swap(aa[X[j]], aa[Y[j]]);
				}
			}
			
			return k + 1;
		}
	}
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:12:43: warning: unused parameter 'M' [-Wunused-parameter]
   12 | int findSwapPairs(int N, int S_tmp[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                       ~~~~^
sorting.cpp:77:1: warning: control reaches end of non-void function [-Wreturn-type]
   77 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 308 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Incorrect 2 ms 468 KB Output isn't correct
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 456 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 456 KB Output isn't correct
2 Halted 0 ms 0 KB -