답안 #804983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
804983 2023-08-03T12:17:13 Z tomruk 정렬하기 (IOI15_sorting) C++17
0 / 100
2 ms 340 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int l = 0,r = M;
	while(l < r){
		int m = (l + r)/2;
		vector<int> v;
		for(int i = 0;i<N;i++){
			v.push_back(S[i]);
		}
		vector<int> ord(N,0);
		iota(ord.begin(),ord.end(),0);
		vector<int> pos = ord;
		for(int i = m-1;i>=0;i--){
			pos[ord[Y[i]]] = X[i];
			pos[ord[X[i]]] = Y[i];
			swap(ord[X[i]],ord[Y[i]]);
		}
		// for(auto u:v){
		// 	cout << u << ' ';
		// }
		// cout << endl;
		int last = -1;
		for(int i = 0;i<m;i++){
			pos[ord[Y[i]]] = X[i];
			pos[ord[X[i]]] = Y[i];
			swap(ord[X[i]],ord[Y[i]]);
			swap(v[X[i]],v[Y[i]]);
			last++;
			while(last < N && last == pos[v[last]]){
				last++;
			}
			if(last < N){
				swap(v[last],v[pos[v[last]]]);
			}
			// for(auto u:v){
			// 	cout << u << ' ';
			// }
			// cout << endl;
		}
		//cout << l << ' ' << r << endl;
		if(is_sorted(v.begin(),v.end())){
			r = m;
		}
		else l = m + 1;
	}
	vector<int> v;
	for(int i = 0;i<N;i++){
		v.push_back(S[i]);
	}
	vector<int> ord(N,0);
	iota(ord.begin(),ord.end(),0);
	vector<int> pos = ord;
	for(int i = l-1;i>=0;i--){
		pos[ord[Y[i]]] = X[i];
		pos[ord[X[i]]] = Y[i];
		swap(ord[X[i]],ord[Y[i]]);
	}
	// for(auto u:v){
	// 	cout << u << ' ';
	// }
	// cout << endl;
	int last = -1;
	for(int i = 0;i<l;i++){
		pos[ord[Y[i]]] = X[i];
		pos[ord[X[i]]] = Y[i];
		swap(ord[X[i]],ord[Y[i]]);
		swap(v[X[i]],v[Y[i]]);
		last++;
		while(last < N && last == pos[v[last]]){
			last++;
		}
		P[i] = Q[i] = 0;
		if(last < N){
			P[i] = last;
			Q[i] = pos[v[last]];
			swap(v[last],v[pos[v[last]]]);
		}
		// for(auto u:v){
		// 	cout << u << ' ';
		// }
		// cout << endl;
	}
	// assert(is_sorted(v.begin(),v.end()));
	return l;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 2 ms 240 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -