#include "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& arr) {
out << "[";
for (int i = 0; i < sz(arr); i++) {
if (i) {
out << ", ";
}
out << arr[i];
}
return out << "]";
}
template <typename A, typename B>
ostream& operator<<(ostream& out, const pair<A, B>& p) {
return out << "(" << p.first << ", " << p.second << ")";
}
vector<pair<int, int>> min_sort(vector<int> arr) {
int n = sz(arr);
vector<int> pos(n);
for (int i = 0; i < n; i++) {
pos[arr[i]] = i;
}
vector<pair<int, int>> ans;
auto do_swap = [&](int u, int v) -> void {
ans.emplace_back(u, v);
swap(arr[u], arr[v]);
swap(pos[arr[u]], pos[arr[v]]);
};
for (int i = 0; i < n; i++) {
if (arr[i] == i) {
continue;
}
do_swap(i, pos[i]);
}
return ans;
}
bool solve_check(const vector<int>& arr, const vector<pair<int, int>>& swaps) {
int n = sz(arr);
vector<int> res(n);
{
auto tmp = arr;
for (auto& [u, v] : swaps) {
swap(tmp[u], tmp[v]);
}
for (int i = 0; i < n; i++) {
res[tmp[i]] = i;
}
}
auto ans_swaps = min_sort(res);
if (sz(ans_swaps) > sz(swaps)) {
return false;
}
return true;
}
optional<vector<pair<int, int>>> solve(vector<int> arr,
const vector<pair<int, int>>& swaps) {
int n = sz(arr);
vector<int> res(n), pos(n);
for (int i = 0; i < n; i++) {
pos[arr[i]] = i;
}
{
auto tmp = arr;
for (auto& [u, v] : swaps) {
swap(tmp[u], tmp[v]);
}
for (int i = 0; i < n; i++) {
res[tmp[i]] = i;
}
}
auto ans_swaps = min_sort(res);
if (sz(ans_swaps) > sz(swaps)) {
return {};
}
vector<pair<int, int>> ans;
auto do_swap = [&](int u, int v, bool swap_res) -> void {
swap(arr[u], arr[v]);
swap(pos[arr[u]], pos[arr[v]]);
if (swap_res) {
swap(res[arr[u]], res[arr[v]]);
}
};
for (int t = 0; t < sz(ans_swaps); t++) {
{
auto& [u, v] = swaps[t];
do_swap(u, v, false);
}
{
auto& [u, v] = ans_swaps[t];
ans.emplace_back(pos[u], pos[v]);
do_swap(pos[u], pos[v], true);
}
}
for (int t = sz(ans_swaps); t < sz(swaps); t++) {
ans.emplace_back(0, 0);
}
return ans;
}
void grade(vector<int> arr,
const vector<pair<int, int>>& swaps,
const vector<pair<int, int>>& ans_swaps) {
assert(sz(ans_swaps) <= sz(swaps));
for (int i = 0; i < sz(ans_swaps); i++) {
if (is_sorted(begin(arr), end(arr))) {
return;
}
{
auto [u, v] = swaps[i];
swap(arr[u], arr[v]);
}
{
auto [u, v] = ans_swaps[i];
swap(arr[u], arr[v]);
}
}
assert(is_sorted(begin(arr), end(arr)));
}
int findSwapPairs(int n,
int p_arr[],
int m,
int swaps_x[],
int swaps_y[],
int out_p[],
int out_q[]) {
vector<int> arr(p_arr, p_arr + n);
vector<pair<int, int>> swaps(m);
for (int i = 0; i < m; i++) {
swaps[i] = {swaps_x[i], swaps_y[i]};
}
int l = 0, r = sz(swaps);
while (l < r) {
int mid = (l + r) / 2;
if (solve_check(arr, vector(swaps.begin(), swaps.begin() + mid))) {
r = mid;
} else {
l = mid + 1;
}
}
auto ans = solve(arr, vector(swaps.begin(), swaps.begin() + l)).value();
for (int i = 0; i < sz(ans); i++) {
auto& [p, q] = ans[i];
out_p[i] = p;
out_q[i] = q;
}
// grade(arr, swaps, ans);
return sz(ans);
}
# |
Verdict |
Execution time |
Memory |
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 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
488 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
488 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
115 ms |
21000 KB |
Output is correct |
16 |
Correct |
128 ms |
21444 KB |
Output is correct |
17 |
Correct |
130 ms |
22432 KB |
Output is correct |
18 |
Correct |
56 ms |
17792 KB |
Output is correct |
19 |
Correct |
92 ms |
18668 KB |
Output is correct |
20 |
Correct |
93 ms |
18960 KB |
Output is correct |
21 |
Correct |
95 ms |
19020 KB |
Output is correct |
22 |
Correct |
111 ms |
22852 KB |
Output is correct |
23 |
Correct |
136 ms |
31944 KB |
Output is correct |
24 |
Correct |
139 ms |
31456 KB |
Output is correct |
25 |
Correct |
139 ms |
31352 KB |
Output is correct |
26 |
Correct |
95 ms |
27420 KB |
Output is correct |
27 |
Correct |
91 ms |
26548 KB |
Output is correct |
28 |
Correct |
130 ms |
31148 KB |
Output is correct |
29 |
Correct |
134 ms |
30640 KB |
Output is correct |
30 |
Correct |
76 ms |
25376 KB |
Output is correct |
31 |
Correct |
134 ms |
31132 KB |
Output is correct |
32 |
Correct |
131 ms |
30880 KB |
Output is correct |
33 |
Correct |
139 ms |
31372 KB |
Output is correct |
34 |
Correct |
135 ms |
31384 KB |
Output is correct |
35 |
Correct |
94 ms |
26824 KB |
Output is correct |
36 |
Correct |
47 ms |
24448 KB |
Output is correct |
37 |
Correct |
137 ms |
32236 KB |
Output is correct |
38 |
Correct |
129 ms |
31016 KB |
Output is correct |
39 |
Correct |
130 ms |
31224 KB |
Output is correct |