#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int a[3009];
int sord[3009];
int cel[3009];
vector<vector<pair<int,int>>> zmiany;
int zmienione[3009];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,odp,flag=1;
cin >> n;
for (int x=1;x<=n;x++){
cin >> a[x];
sord[x] = a[x];
}
sort(sord+1,sord+n+1);
for (int x=1;x<=n;x++){
cel[sord[x]] = x;
}
while(flag){
flag = 0;
zmiany.push_back({});
for (int x=0;x<=n;x++){
zmienione[x] = 0;
}
for (int x=1;x<=n;x++){
if (cel[a[x]]!=x && zmienione[x] == 0 && zmienione[cel[a[x]]] == 0){
zmiany.back().push_back({x,cel[a[x]]});
zmienione[x] = 1;
zmienione[cel[a[x]]] = 1;
flag = 1;
}
}
for (pair<int,int> zmien:zmiany.back()){
swap(a[zmien.first],a[zmien.second]);
}
}
cout << zmiany.size()-1 << '\n';
vector<int> temp;
for (vector<pair<int,int>> x:zmiany){
temp.clear();
if (x.size()==0){
continue;
}
for (int y=0;y<x.size();y++){
temp.push_back(x[y].first);
}
for (int y=x.size()-1;y>=0;y--){
temp.push_back(x[y].second);
}
cout << temp.size() << '\n';
for (int y:temp){
cout << y << ' ';
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long int ll; int a[3009]; int sord[3009]; int cel[3009]; vector<vector<pair<int,int>>> zmiany; int zmienione[3009]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,odp,flag=1; cin >> n; for (int x=1;x<=n;x++){ cin >> a[x]; sord[x] = a[x]; } sort(sord+1,sord+n+1); for (int x=1;x<=n;x++){ cel[sord[x]] = x; } while(flag){ flag = 0; zmiany.push_back({}); for (int x=0;x<=n;x++){ zmienione[x] = 0; } for (int x=1;x<=n;x++){ if (cel[a[x]]!=x && zmienione[x] == 0 && zmienione[cel[a[x]]] == 0){ zmiany.back().push_back({x,cel[a[x]]}); zmienione[x] = 1; zmienione[cel[a[x]]] = 1; flag = 1; } } for (pair<int,int> zmien:zmiany.back()){ swap(a[zmien.first],a[zmien.second]); } } cout << zmiany.size()-1 << '\n'; vector<int> temp; for (vector<pair<int,int>> x:zmiany){ temp.clear(); if (x.size()==0){ continue; } for (int y=0;y<x.size();y++){ temp.push_back(x[y].first); } for (int y=x.size()-1;y>=0;y--){ temp.push_back(x[y].second); } cout << temp.size() << '\n'; for (int y:temp){ cout << y << ' '; } cout << '\n'; } return 0; } |
English