// Fotografia FOT; Patryk Grabowski xiv lo
#include<bits/stdc++.h>
using namespace std;
int main_wzo() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n;
cin >> n;
int wzrosty[n], sorted[n];
int pozycje[3007];
int x;
for(int i = 0; i < n; i++) {
cin >> x;
wzrosty[i] = x;
sorted[i] = x;
pozycje[x] = i;
}
sort(sorted, sorted + n);
int pozycje_sorted[3007];
for(int i = 0; i < n; i++)
pozycje_sorted[sorted[i]] = i;
vector<int> poc, kon;
bool niegotowe = true;
stringstream odp;
int ilosc = 0;
bool odw[3007];
while(niegotowe) {
/*
for(int i : wzrosty)
cout << i << " ";
cout << '\n';
*/
poc.clear();
kon.clear();
niegotowe = false;
for(int i = 0; i < 3007; i++)
odw[i] = false;
for (int i = 0; i < n; i++) {
if (sorted[i] == wzrosty[i]) continue;
niegotowe = true;
if(odw[i] || odw[pozycje_sorted[wzrosty[i]]])
continue;
poc.push_back(i);
kon.push_back(pozycje_sorted[wzrosty[i]]);
swap(wzrosty[i], wzrosty[pozycje_sorted[wzrosty[i]]]);
odw[i] = true;
odw[pozycje_sorted[wzrosty[i]]] = true;
}
if(!niegotowe)
break;
ilosc++;
odp << poc.size() + kon.size() << "\n";
for(auto i : poc)
odp << i + 1 << " ";
reverse(kon.begin(), kon.end());
for(auto i : kon)
odp << i + 1 << " ";
odp << '\n';
}
//cout << '\n';
cout << ilosc << '\n' << odp.str();
return 0;
}
int main() {
main_wzo();
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 | // Fotografia FOT; Patryk Grabowski xiv lo #include<bits/stdc++.h> using namespace std; int main_wzo() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; int wzrosty[n], sorted[n]; int pozycje[3007]; int x; for(int i = 0; i < n; i++) { cin >> x; wzrosty[i] = x; sorted[i] = x; pozycje[x] = i; } sort(sorted, sorted + n); int pozycje_sorted[3007]; for(int i = 0; i < n; i++) pozycje_sorted[sorted[i]] = i; vector<int> poc, kon; bool niegotowe = true; stringstream odp; int ilosc = 0; bool odw[3007]; while(niegotowe) { /* for(int i : wzrosty) cout << i << " "; cout << '\n'; */ poc.clear(); kon.clear(); niegotowe = false; for(int i = 0; i < 3007; i++) odw[i] = false; for (int i = 0; i < n; i++) { if (sorted[i] == wzrosty[i]) continue; niegotowe = true; if(odw[i] || odw[pozycje_sorted[wzrosty[i]]]) continue; poc.push_back(i); kon.push_back(pozycje_sorted[wzrosty[i]]); swap(wzrosty[i], wzrosty[pozycje_sorted[wzrosty[i]]]); odw[i] = true; odw[pozycje_sorted[wzrosty[i]]] = true; } if(!niegotowe) break; ilosc++; odp << poc.size() + kon.size() << "\n"; for(auto i : poc) odp << i + 1 << " "; reverse(kon.begin(), kon.end()); for(auto i : kon) odp << i + 1 << " "; odp << '\n'; } //cout << '\n'; cout << ilosc << '\n' << odp.str(); return 0; } int main() { main_wzo(); return 0; } |
English