#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> findcycles(vector<int>& p, int n){
vector<vector<int>> cykle;
int x = 1;
int siz = 0;
vector<int> vis(n+1);
for (int i = 1; i <= n; i++){
if (!vis[i]){
vector<int> cykl;
cykl.push_back(i);
vis[i] = 1;
x = i;
siz = 1;
while (!vis[p[x]]){
x = p[x];
vis[x] = 1;
siz += 1;
cykl.push_back(x);
}
if (siz-1) cykle.push_back(cykl);
}
}
return cykle;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<int> a(n+1);
vector<int> kt(3001);
for (int i = 1; i <= n; i++){
cin >> a[i];
kt[a[i]] = i;
}
vector<int> p(n+1);
int pos = 1;
for (int i = 1; i <= 3000; i++){
if (kt[i]) p[kt[i]] = pos++;
}
vector<vector<int>> rundy;
vector<vector<int>> cykle = findcycles(p, n);
while (cykle.size()){
vector<int> lewo;
vector<int> prawo;
vector<int> runda;
for (auto cykl: cykle){
int low = 0;
int high = cykl.size()-1;
while (low < high){
int tmp = p[cykl[low]];
p[cykl[low]] = p[cykl[high]];
p[cykl[high]] = tmp;
lewo.push_back(cykl[low]);
prawo.push_back(cykl[high]);
low++;
high--;
}
}
for (auto u: lewo) runda.push_back(u);
reverse(prawo.begin(), prawo.end());
for (auto u: prawo) runda.push_back(u);
rundy.push_back(runda);
cykle = findcycles(p, n);
}
cout << rundy.size() << "\n";
for (auto u: rundy){
cout << u.size() << "\n";
for (auto x: u) cout << x << " ";
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 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include <bits/stdc++.h> using namespace std; vector<vector<int>> findcycles(vector<int>& p, int n){ vector<vector<int>> cykle; int x = 1; int siz = 0; vector<int> vis(n+1); for (int i = 1; i <= n; i++){ if (!vis[i]){ vector<int> cykl; cykl.push_back(i); vis[i] = 1; x = i; siz = 1; while (!vis[p[x]]){ x = p[x]; vis[x] = 1; siz += 1; cykl.push_back(x); } if (siz-1) cykle.push_back(cykl); } } return cykle; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n+1); vector<int> kt(3001); for (int i = 1; i <= n; i++){ cin >> a[i]; kt[a[i]] = i; } vector<int> p(n+1); int pos = 1; for (int i = 1; i <= 3000; i++){ if (kt[i]) p[kt[i]] = pos++; } vector<vector<int>> rundy; vector<vector<int>> cykle = findcycles(p, n); while (cykle.size()){ vector<int> lewo; vector<int> prawo; vector<int> runda; for (auto cykl: cykle){ int low = 0; int high = cykl.size()-1; while (low < high){ int tmp = p[cykl[low]]; p[cykl[low]] = p[cykl[high]]; p[cykl[high]] = tmp; lewo.push_back(cykl[low]); prawo.push_back(cykl[high]); low++; high--; } } for (auto u: lewo) runda.push_back(u); reverse(prawo.begin(), prawo.end()); for (auto u: prawo) runda.push_back(u); rundy.push_back(runda); cykle = findcycles(p, n); } cout << rundy.size() << "\n"; for (auto u: rundy){ cout << u.size() << "\n"; for (auto x: u) cout << x << " "; cout << "\n"; } return 0; } |
English