-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1698.cpp
47 lines (45 loc) · 1 KB
/
1698.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, k, a[N];
bool mark[N];
vector<pair<int, int>> rounds[2];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
if (mark[i]) continue;
vector<int> cycle;
for (int j = i; !mark[j]; j = a[j]) {
cycle.emplace_back(j);
mark[j] = true;
}
for (int l = 1, r = (int)cycle.size() - 1; l < r; ++l, --r) {
rounds[k].emplace_back(cycle[l], cycle[r]);
swap(a[cycle[l]], a[cycle[r]]);
}
}
if (!rounds[k].empty()) {
++k;
}
for (int i = 1; i <= n; ++i) {
if (a[i] != i && a[i] < i) {
assert(a[a[i]] == i);
rounds[k].emplace_back(i, a[i]);
}
}
if (!rounds[k].empty()) {
++k;
}
cout << k << "\n";
for (int i = 0; i < k; ++i) {
cout << rounds[i].size() << "\n";
for (auto [x, y]: rounds[i]) {
cout << x << " " << y << "\n";
}
}
return 0;
}