#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3007;
int n;
int t[MAXN];
map<int,int>mapa;
int indeks[MAXN];
vector<vector<int>>wyn;
bool uzyte[MAXN];
void ustawindeksy(){
for (int i = 1; i <= n; i++){
indeks[t[i]] = i;
}
}
bool czyok(){
for (int i = 1; i <= n; i++){
if (i != t[i]){
return 0;
}
}
return 1;
}
void wypisz(){
cout<<"TABLICA\n";
for (int i = 1; i <= n; i++){
cout<<t[i]<<" ";
}
cout<<"\n";
cout<<"INDEKSY\n";
for (int i = 1; i <= n; i++){
cout<<i<<" ";
}
cout<<"\n";
for (int i = 1; i <= n; i++){
cout<<indeks[i]<<" ";
}
cout<<"\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
vector<int>v;
for (int i = 1; i <= n; i++){
cin>>t[i];
v.push_back(t[i]);
}
sort(v.begin(), v.end());
int wsk = 1;
for (auto i : v){
mapa[i] = wsk++;
}
for (int i = 1; i <= n; i++){
t[i] = mapa[t[i]];
}
//wypisz();
vector<int>pocz;
vector<int>kon;
vector<int>ter;
vector<int>poczpom;
vector<int>konpom;
int ile;
while (!czyok()){
//cerr<<'a';
ustawindeksy();
//wypisz();
ile=0;
for (int s = 1; s <= n; s++){
for (int i = 1; i <= n; i++){
uzyte[i] = 0;
}
for (int i = s; i <= n; i++){
if (!uzyte[i] && !uzyte[indeks[i]] && i != t[i]){
uzyte[i] = 1;
uzyte[indeks[i]] = 1;
poczpom.push_back(i);
konpom.push_back(indeks[i]);
}
}
for (int i = 1; i < s; i++){
if (!uzyte[i] && !uzyte[indeks[i]] && i != t[i]){
uzyte[i] = 1;
uzyte[indeks[i]] = 1;
poczpom.push_back(i);
konpom.push_back(indeks[i]);
}
}
if (poczpom.size() > pocz.size()){
pocz = poczpom;
kon = konpom;
}
poczpom.clear();
konpom.clear();
}
/*
for (auto i : pocz){
cout<<i<<" ";
}
cout<<"\n";
for (auto i : kon){
cout<<i<<" ";
}
cout<<"\n\n";
*/
for (int i = 0; i < pocz.size(); i++){
//cout<<"SWAP "<<t[pocz[i]]<<" "<<t[kon[i]]<<" "<<pocz[i]<<" "<<kon[i]<<"\n";
swap(t[pocz[i]], t[kon[i]]);
ter.push_back(pocz[i]);
}
for (int i = pocz.size()-1; i >= 0; i--){
ter.push_back(kon[i]);
}
wyn.push_back(ter);
pocz.clear();
kon.clear();
ter.clear();
for (int i = 1; i <= n; i++){
uzyte[i] = 0;
}
//cerr<<'b';
}
cout<<wyn.size()<<"\n";
for (int i = 0; i < wyn.size(); i++){
cout<<wyn[i].size()<<"\n";
for (auto j : wyn[i]){
cout<<j<<" ";
}
cout<<"\n";
}
}
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #include <bits/stdc++.h> using namespace std; const int MAXN = 3007; int n; int t[MAXN]; map<int,int>mapa; int indeks[MAXN]; vector<vector<int>>wyn; bool uzyte[MAXN]; void ustawindeksy(){ for (int i = 1; i <= n; i++){ indeks[t[i]] = i; } } bool czyok(){ for (int i = 1; i <= n; i++){ if (i != t[i]){ return 0; } } return 1; } void wypisz(){ cout<<"TABLICA\n"; for (int i = 1; i <= n; i++){ cout<<t[i]<<" "; } cout<<"\n"; cout<<"INDEKSY\n"; for (int i = 1; i <= n; i++){ cout<<i<<" "; } cout<<"\n"; for (int i = 1; i <= n; i++){ cout<<indeks[i]<<" "; } cout<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; vector<int>v; for (int i = 1; i <= n; i++){ cin>>t[i]; v.push_back(t[i]); } sort(v.begin(), v.end()); int wsk = 1; for (auto i : v){ mapa[i] = wsk++; } for (int i = 1; i <= n; i++){ t[i] = mapa[t[i]]; } //wypisz(); vector<int>pocz; vector<int>kon; vector<int>ter; vector<int>poczpom; vector<int>konpom; int ile; while (!czyok()){ //cerr<<'a'; ustawindeksy(); //wypisz(); ile=0; for (int s = 1; s <= n; s++){ for (int i = 1; i <= n; i++){ uzyte[i] = 0; } for (int i = s; i <= n; i++){ if (!uzyte[i] && !uzyte[indeks[i]] && i != t[i]){ uzyte[i] = 1; uzyte[indeks[i]] = 1; poczpom.push_back(i); konpom.push_back(indeks[i]); } } for (int i = 1; i < s; i++){ if (!uzyte[i] && !uzyte[indeks[i]] && i != t[i]){ uzyte[i] = 1; uzyte[indeks[i]] = 1; poczpom.push_back(i); konpom.push_back(indeks[i]); } } if (poczpom.size() > pocz.size()){ pocz = poczpom; kon = konpom; } poczpom.clear(); konpom.clear(); } /* for (auto i : pocz){ cout<<i<<" "; } cout<<"\n"; for (auto i : kon){ cout<<i<<" "; } cout<<"\n\n"; */ for (int i = 0; i < pocz.size(); i++){ //cout<<"SWAP "<<t[pocz[i]]<<" "<<t[kon[i]]<<" "<<pocz[i]<<" "<<kon[i]<<"\n"; swap(t[pocz[i]], t[kon[i]]); ter.push_back(pocz[i]); } for (int i = pocz.size()-1; i >= 0; i--){ ter.push_back(kon[i]); } wyn.push_back(ter); pocz.clear(); kon.clear(); ter.clear(); for (int i = 1; i <= n; i++){ uzyte[i] = 0; } //cerr<<'b'; } cout<<wyn.size()<<"\n"; for (int i = 0; i < wyn.size(); i++){ cout<<wyn[i].size()<<"\n"; for (auto j : wyn[i]){ cout<<j<<" "; } cout<<"\n"; } } |
English