#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<pair<int, int>> ans;
for (int i = 0; i < n; i++) {
if (arr[i] == i) {
continue;
}
int u = int(find(begin(arr), end(arr), i) - arr.begin());
ans.emplace_back(i, u);
swap(arr[i], arr[u]);
}
return ans;
}
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(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 |
1 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
824 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
816 KB |
Output is correct |
26 |
Correct |
2 ms |
824 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
468 KB |
Output is correct |
2 |
Correct |
17 ms |
596 KB |
Output is correct |
3 |
Correct |
14 ms |
588 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
4 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
596 KB |
Output is correct |
7 |
Correct |
10 ms |
596 KB |
Output is correct |
8 |
Correct |
15 ms |
600 KB |
Output is correct |
9 |
Correct |
17 ms |
580 KB |
Output is correct |
10 |
Correct |
16 ms |
596 KB |
Output is correct |
11 |
Correct |
15 ms |
596 KB |
Output is correct |
12 |
Correct |
10 ms |
616 KB |
Output is correct |
13 |
Correct |
15 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
468 KB |
Output is correct |
2 |
Correct |
17 ms |
596 KB |
Output is correct |
3 |
Correct |
14 ms |
588 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
4 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
596 KB |
Output is correct |
7 |
Correct |
10 ms |
596 KB |
Output is correct |
8 |
Correct |
15 ms |
600 KB |
Output is correct |
9 |
Correct |
17 ms |
580 KB |
Output is correct |
10 |
Correct |
16 ms |
596 KB |
Output is correct |
11 |
Correct |
15 ms |
596 KB |
Output is correct |
12 |
Correct |
10 ms |
616 KB |
Output is correct |
13 |
Correct |
15 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
520 KB |
Output is correct |
15 |
Execution timed out |
1030 ms |
23372 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |