답안 #387233

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
387233 2021-04-08T07:01:45 Z alireza_kaviani 정렬하기 (IOI15_sorting) C++11
0 / 100
22 ms 6124 KB
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;

#define SZ(x)			int(x.size())

const int MAXN = 2e5 + 10;

int A[MAXN] , to[MAXN] , pos[MAXN] , mark[MAXN];
vector<int> vec , adj[MAXN];

void DFS(int v){
	mark[v] = 1;
	vec.push_back(v);
	for(int u : adj[v]){
		if(!mark[u])	DFS(u);
	}
}

int	check(int N, int M, int X[], int Y[], int P[], int Q[]) {
	int cnt = 0;
	fill(mark , mark + MAXN , 0);
	fill(adj , adj + MAXN , vector<int>());
    for(int i = 0 ; i < N ; i++)	to[i] = pos[i] = i;
	for(int i = 0 ; i < M ; i++){
		swap(A[X[i]] , A[Y[i]]);
	}
	for(int i = 0 ; i < N ; i++){
		adj[i].push_back(A[i]);
	}
	for(int i = 0 ; i < N ; i++){
		if(!mark[i]){
			vec.clear();
			DFS(i);
			for(int j = 1 ; j < SZ(vec) ; j++){
				if(cnt == M)	return 0;
				P[cnt] = vec[0];
				Q[cnt] = vec[j];
				cnt++;
			}
		}
	}
	for(int i = M - 1 ; i >= 0 ; i--){
		if(i < cnt){
			P[i] = to[P[i]];
			Q[i] = to[Q[i]];
		}
		to[pos[X[i]]] = Y[i];
		to[pos[Y[i]]] = X[i];
		swap(pos[X[i]] , pos[Y[i]]);
	}
	return 1;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]){
	int l = -1 , r = M;
	while(r - l > 1){
		int mid = l + r >> 1;
		for(int i = 0 ; i < N ; i++)	A[i] = S[i];
		fill(P , P + M , 0);
		fill(Q , Q + M , 0);
		if(check(N , mid , X , Y , P , Q))	r = mid;
		else	l = mid;
	}
	return r;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:58:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   58 |   int mid = l + r >> 1;
      |             ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5868 KB Output is correct
2 Correct 7 ms 5868 KB Output is correct
3 Incorrect 7 ms 5868 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5868 KB Output is correct
2 Correct 7 ms 5868 KB Output is correct
3 Incorrect 7 ms 5868 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 5868 KB Output is correct
2 Correct 18 ms 5868 KB Output is correct
3 Incorrect 18 ms 5868 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5868 KB Output is correct
2 Correct 7 ms 5868 KB Output is correct
3 Incorrect 7 ms 5868 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 6124 KB Output is correct
2 Correct 22 ms 6124 KB Output is correct
3 Incorrect 22 ms 6124 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 6124 KB Output is correct
2 Correct 22 ms 6124 KB Output is correct
3 Incorrect 22 ms 6124 KB Output isn't correct
4 Halted 0 ms 0 KB -