#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> oset;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
oset st;
vector <int> a(n);
for (int i = 0; i < n; i++){
cin >> a[i];
st.insert(a[i]);
}
for (int i = 0; i < n; i++){
a[i] = st.order_of_key(a[i]);
}
vector <vector <int>> ans;
vector <int> l, r;
vector <bool> used(n);
while (!is_sorted(a.begin(), a.end())){
for (int i = 0; i < n; i++){
if (a[i] != i && !used[a[i]] && !used[i]){
l.push_back(a[i]);
r.push_back(i);
used[a[i]] = true;
used[i] = true;
}
}
for (int i = 0; i < (int) l.size(); i++){
swap(a[l[i]], a[r[i]]);
}
ans.push_back({});
ans.back().insert(ans.back().end(), l.begin(), l.end());
ans.back().insert(ans.back().end(), r.rbegin(), r.rend());
l.clear();
r.clear();
fill(used.begin(), used.end(), false);
}
cout << ans.size() << "\n";
for (vector <int> v : ans){
cout << v.size() << "\n";
for (int x : v){
cout << x + 1 << " ";
}
cout << "\n";
}
return 0;
}
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 48 49 50 51 52 53 54 55 56 57 58 59 | #include <bits/stdc++.h> typedef long long int lli; using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> oset; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; oset st; vector <int> a(n); for (int i = 0; i < n; i++){ cin >> a[i]; st.insert(a[i]); } for (int i = 0; i < n; i++){ a[i] = st.order_of_key(a[i]); } vector <vector <int>> ans; vector <int> l, r; vector <bool> used(n); while (!is_sorted(a.begin(), a.end())){ for (int i = 0; i < n; i++){ if (a[i] != i && !used[a[i]] && !used[i]){ l.push_back(a[i]); r.push_back(i); used[a[i]] = true; used[i] = true; } } for (int i = 0; i < (int) l.size(); i++){ swap(a[l[i]], a[r[i]]); } ans.push_back({}); ans.back().insert(ans.back().end(), l.begin(), l.end()); ans.back().insert(ans.back().end(), r.rbegin(), r.rend()); l.clear(); r.clear(); fill(used.begin(), used.end(), false); } cout << ans.size() << "\n"; for (vector <int> v : ans){ cout << v.size() << "\n"; for (int x : v){ cout << x + 1 << " "; } cout << "\n"; } return 0; } |
English