Submission #1004609

# Submission time Handle Problem Language Result Execution time Memory
1004609 2024-06-21T10:28:05 Z phoenix Sorting (IOI15_sorting) C++17
100 / 100
181 ms 20284 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[]) {
	vector<pair<int, int>> res;
	
	function<bool(int)> check = [&](int len) -> bool {
		res.resize(len);
		vector<int> s(n), pos(n);
		for (int i = 0; i < n; i++) {
			s[i] = S[i];
			pos[S[i]] = i;
		}

		vector<int> p(n), q(n);
		iota(p.begin(), p.end(), 0);
		iota(q.begin(), q.end(), 0);
		auto do_swap = [&](int i, int j) {
			swap(p[i], p[j]);
			swap(q[p[i]], q[p[j]]);
		};
		for (int i = len - 1; i >= 0; i--) {
			do_swap(X[i], Y[i]);
		}

		int l = n - 1;
		for (int i = 0; i < len; i++) {
			do_swap(X[i], Y[i]);
			swap(s[X[i]], s[Y[i]]);
			swap(pos[s[X[i]]], pos[s[Y[i]]]);

			while (l >= 0 && q[l] == pos[l]) 
				l--;
			if (l == -1) 
				res[i] = {0, 0};
			else {
				res[i] = {pos[l], q[l]};
			}
			swap(s[res[i].first], s[res[i].second]);
			swap(pos[s[res[i].first]], pos[s[res[i].second]]);
		}
		while (l >= 0 && q[l] == pos[l]) l--;
		return (l == -1);
	};

	int l = -1, r = m + 1;
	while (r - l > 1) {
		int mid = (l + r) / 2;
		if (check(mid)) 
			r = mid;
		else 
			l = mid;
	}
	check(r);
	for (int i = 0; i < r; i++) {
		P[i] = res[i].first;
		Q[i] = res[i].second;
	}
	return r;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 356 KB Output is correct
5 Correct 0 ms 356 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 356 KB Output is correct
5 Correct 0 ms 356 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 356 KB Output is correct
12 Correct 0 ms 356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 356 KB Output is correct
4 Correct 0 ms 356 KB Output is correct
5 Correct 0 ms 356 KB Output is correct
6 Correct 0 ms 356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 356 KB Output is correct
5 Correct 0 ms 356 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 356 KB Output is correct
12 Correct 0 ms 356 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 356 KB Output is correct
16 Correct 0 ms 356 KB Output is correct
17 Correct 0 ms 356 KB Output is correct
18 Correct 0 ms 356 KB Output is correct
19 Correct 0 ms 356 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 1 ms 616 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 608 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 608 KB Output is correct
26 Correct 1 ms 612 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 452 KB Output is correct
2 Correct 1 ms 608 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 452 KB Output is correct
2 Correct 1 ms 608 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 132 ms 17796 KB Output is correct
16 Correct 157 ms 18228 KB Output is correct
17 Correct 174 ms 19200 KB Output is correct
18 Correct 74 ms 18628 KB Output is correct
19 Correct 141 ms 19812 KB Output is correct
20 Correct 140 ms 20244 KB Output is correct
21 Correct 133 ms 20232 KB Output is correct
22 Correct 151 ms 14512 KB Output is correct
23 Correct 140 ms 19936 KB Output is correct
24 Correct 172 ms 19812 KB Output is correct
25 Correct 179 ms 19400 KB Output is correct
26 Correct 130 ms 20284 KB Output is correct
27 Correct 126 ms 19872 KB Output is correct
28 Correct 165 ms 20000 KB Output is correct
29 Correct 159 ms 19532 KB Output is correct
30 Correct 108 ms 20052 KB Output is correct
31 Correct 162 ms 19956 KB Output is correct
32 Correct 152 ms 19328 KB Output is correct
33 Correct 171 ms 19624 KB Output is correct
34 Correct 154 ms 19540 KB Output is correct
35 Correct 148 ms 19400 KB Output is correct
36 Correct 68 ms 19992 KB Output is correct
37 Correct 158 ms 20228 KB Output is correct
38 Correct 181 ms 19444 KB Output is correct
39 Correct 169 ms 19544 KB Output is correct