답안 #835364

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
835364 2023-08-23T13:47:00 Z Valaki2 정렬하기 (IOI15_sorting) C++14
100 / 100
185 ms 35904 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 2e5;

int n, m;

vector<pii > ans;

void upd(pii cur_swap, vector<int> &pos, vector<int> &which, vector<int> &to, vector<int> &where) {
	// cur_swap: position of two places
	int pos_a = cur_swap.fi, pos_b = cur_swap.se;
	int which_a = which[pos_a], which_b = which[pos_b];
	swap(where[which_a], where[which_b]);
	swap(to[pos_a], to[pos_b]);
	swap(which[pos_a], which[pos_b]);
	swap(pos[which_a], pos[which_b]);
}

bool ok(int k, vector<int> which, vector<pii > swaps, bool create_swap_order) {
	swaps.resize(k);
	if(k == 0) {
		for(int i = 1; i <= n; i++) {
			if(which[i] != i) {
				return false;
			}
		}
		return true;
	}
	vector<int> to(1 + n, 0);
	for(int i = 1; i <= n; i++) {
		to[i] = i;
	}
	for(auto it = swaps.rbegin(); it != swaps.rend(); it++) {
		pii p = *it;
		swap(to[p.fi], to[p.se]);
	}
	vector<int> where(1 + n, 0);
	vector<int> pos(1 + n, 0);
	for(int i = 1; i <= n; i++) {
		where[to[i]] = i;
		pos[which[i]] = i;
	}
	vector<int> perm(1 + n, 0);
	for(int i = 1; i <= n; i++) {
		perm[pos[i]] = where[i];
	}
	vector<pii > ans_elem;
	vector<bool> vis(1 + n, false);
	int cnt = 0;
	for(int i = 1; i <= n; i++) {
		if(!vis[i]) {
			int x = i;
			int depth = 0;
			vis[x] = true;
			while(!vis[perm[x]]) {
				if(create_swap_order) {
					ans_elem.pb(mp(which[x], which[perm[x]]));
				}
				x = perm[x];
				vis[x] = true;
				depth++;
			}
			cnt += depth;
		}
	}
	if(!create_swap_order) {
		return (cnt <= k);
	}
	while(ans_elem.size() < k) {
		ans_elem.pb(mp(1, 1));
	}
	ans.resize(k);
	for(int i = 0; i < k; i++) {
		pii cur_swap = swaps[i];
		upd(cur_swap, pos, which, to, where);
		ans[i] = mp(pos[ans_elem[i].fi], pos[ans_elem[i].se]);
		upd(ans[i], pos, which, to, where);
	}
	return true;
}

vector<int> which;
vector<pii > swaps;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n = N;
	which.assign(1 + n, 0);
	for(int i = 1; i <= n; i++) {
		which[i] = S[i - 1] + 1;
	}
	m = M;
	for(int i = 0; i < m; i++) {
		swaps.pb(mp(X[i] + 1, Y[i] + 1));
	}
	int l = -1, r = m + 1;
	while(l < r - 1) {
		int mid = (l + r) / 2;
		if(ok(mid, which, swaps, false)) {
			r = mid;
		} else {
			l = mid;
		}
	}
	int k = r;
	if(k == 0) {
		// no need to modify P/Q here
		return 0;
	}
	ok(k, which, swaps, true);
	for(int i = 0; i < k; i++) {
		P[i] = ans[i].fi - 1;
		Q[i] = ans[i].se - 1;
	}
	return k;
}

Compilation message

sorting.cpp: In function 'bool ok(int, std::vector<int>, std::vector<std::pair<int, int> >, bool)':
sorting.cpp:78:24: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   78 |  while(ans_elem.size() < k) {
      |        ~~~~~~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 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 340 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 0 ms 212 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 1 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 340 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 0 ms 212 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 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 716 KB Output is correct
24 Correct 2 ms 696 KB Output is correct
25 Correct 2 ms 724 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 572 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 568 KB Output is correct
8 Correct 2 ms 568 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 572 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 568 KB Output is correct
8 Correct 2 ms 568 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 147 ms 31596 KB Output is correct
16 Correct 139 ms 32276 KB Output is correct
17 Correct 157 ms 33976 KB Output is correct
18 Correct 79 ms 26444 KB Output is correct
19 Correct 138 ms 31648 KB Output is correct
20 Correct 145 ms 32464 KB Output is correct
21 Correct 143 ms 32532 KB Output is correct
22 Correct 133 ms 29400 KB Output is correct
23 Correct 155 ms 35096 KB Output is correct
24 Correct 165 ms 34812 KB Output is correct
25 Correct 159 ms 34608 KB Output is correct
26 Correct 157 ms 32444 KB Output is correct
27 Correct 134 ms 31400 KB Output is correct
28 Correct 181 ms 34468 KB Output is correct
29 Correct 185 ms 34348 KB Output is correct
30 Correct 117 ms 30124 KB Output is correct
31 Correct 156 ms 34964 KB Output is correct
32 Correct 143 ms 33844 KB Output is correct
33 Correct 156 ms 34808 KB Output is correct
34 Correct 159 ms 34772 KB Output is correct
35 Correct 135 ms 31212 KB Output is correct
36 Correct 72 ms 28292 KB Output is correct
37 Correct 162 ms 35904 KB Output is correct
38 Correct 154 ms 34960 KB Output is correct
39 Correct 150 ms 34412 KB Output is correct