#include <bits/stdc++.h>
#include <iterator>
using namespace std;
int n, cnt, x;
pair <int, int> t[3007];
pair <int, int> desire[3007];
vector <pair <int, int>> v[3007];
vector <int> cope;
bool odw[3007];
int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> t[i].first;
desire[i] = {t[i].first, i};
}
sort(desire + 1, desire + n + 1);
for(int i = 1; i <= n; i++){
// cout << desire[i].first << ' ' << desire[i].second << '\n';
t[desire[i].second].second = i;
if(desire[i].first != t[i].first)
cnt++;
}
// for(int i = 1; i <= n; i++)
// cout << t[i].first << ' ' << t[i].second << '\n';
while(cnt){
x++;
for(int i = 1; i <= n; i++){
int sex = i;
while(!odw[sex]){
cope.push_back(sex);
odw[sex] = 1;
sex = t[sex].second;
}
for(int j = 0; j < cope.size()/2; j++){
v[x].push_back({cope[j], cope[cope.size()-1-j]});
swap(t[cope[j]], t[cope[cope.size()-1-j]]);
}
cope.clear();
}
cnt = 0;
for(int i = 1; i <= n; i++){
odw[i] = 0;
if(i != t[i].second)
cnt++;
}
}
cout << x << '\n';
for(int j = 1; j <= x; j++){
cout << v[j].size() * 2 << '\n';
for(int i = 0; i < v[j].size(); i++)
cout << v[j][i].first << ' ';
for(int i = v[j].size()-1; i >= 0; i--)
cout << v[j][i].second << ' ';
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 60 61 62 63 64 65 66 67 | #include <bits/stdc++.h> #include <iterator> using namespace std; int n, cnt, x; pair <int, int> t[3007]; pair <int, int> desire[3007]; vector <pair <int, int>> v[3007]; vector <int> cope; bool odw[3007]; int main(){ // ios_base::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); cin >> n; for(int i = 1; i <= n; i++){ cin >> t[i].first; desire[i] = {t[i].first, i}; } sort(desire + 1, desire + n + 1); for(int i = 1; i <= n; i++){ // cout << desire[i].first << ' ' << desire[i].second << '\n'; t[desire[i].second].second = i; if(desire[i].first != t[i].first) cnt++; } // for(int i = 1; i <= n; i++) // cout << t[i].first << ' ' << t[i].second << '\n'; while(cnt){ x++; for(int i = 1; i <= n; i++){ int sex = i; while(!odw[sex]){ cope.push_back(sex); odw[sex] = 1; sex = t[sex].second; } for(int j = 0; j < cope.size()/2; j++){ v[x].push_back({cope[j], cope[cope.size()-1-j]}); swap(t[cope[j]], t[cope[cope.size()-1-j]]); } cope.clear(); } cnt = 0; for(int i = 1; i <= n; i++){ odw[i] = 0; if(i != t[i].second) cnt++; } } cout << x << '\n'; for(int j = 1; j <= x; j++){ cout << v[j].size() * 2 << '\n'; for(int i = 0; i < v[j].size(); i++) cout << v[j][i].first << ' '; for(int i = v[j].size()-1; i >= 0; i--) cout << v[j][i].second << ' '; cout << '\n'; } return 0; } |
English