#include <bits/stdc++.h>
#define ft first
#define sc second
#define pb push_back
#define FOR(i,n) for(int i=0; i<n; i++)
#define watch(x) cout << (#x) << " == " << (x) << endl
typedef long long ll;
typedef long double ld;
using namespace std;
const ll N =3e3;
pair<int,int> tab[N+5];
int sorted[N+5];
bool moved[N+5];
vector<vector<int>> all;
vector<int> l, r;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
FOR(i,n){
cin>>tab[i].ft;
sorted[i] = tab[i].ft;
}
sort(sorted, sorted+n);
FOR(i,n){
tab[i].sc = lower_bound(sorted, sorted+n, tab[i].ft) - sorted;
}
int start=0;
while(1){
while (start<n && tab[start].ft == sorted[start]){
start++;
}
if (start >= n) {break;}
fill(moved, moved+n, 0);
l.clear();
r.clear();
for (int i=start; i<n; i++){
if (tab[i].ft!=sorted[i] && moved[i]==0 && moved[tab[i].sc]==0){
moved[i]=1;
moved[tab[i].sc]=1;
l.pb(i+1);
r.pb(tab[i].sc+1);
swap(tab[i], tab[tab[i].sc]);
}
}
reverse(r.begin(), r.end());
l.insert(l.end(), r.begin(), r.end());
all.pb(l);
}
cout<<all.size()<<"\n";
for (auto runda : all){
cout<<runda.size()<<"\n";
for (int p : runda){
cout<<p<<" ";
}
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 | #include <bits/stdc++.h> #define ft first #define sc second #define pb push_back #define FOR(i,n) for(int i=0; i<n; i++) #define watch(x) cout << (#x) << " == " << (x) << endl typedef long long ll; typedef long double ld; using namespace std; const ll N =3e3; pair<int,int> tab[N+5]; int sorted[N+5]; bool moved[N+5]; vector<vector<int>> all; vector<int> l, r; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; FOR(i,n){ cin>>tab[i].ft; sorted[i] = tab[i].ft; } sort(sorted, sorted+n); FOR(i,n){ tab[i].sc = lower_bound(sorted, sorted+n, tab[i].ft) - sorted; } int start=0; while(1){ while (start<n && tab[start].ft == sorted[start]){ start++; } if (start >= n) {break;} fill(moved, moved+n, 0); l.clear(); r.clear(); for (int i=start; i<n; i++){ if (tab[i].ft!=sorted[i] && moved[i]==0 && moved[tab[i].sc]==0){ moved[i]=1; moved[tab[i].sc]=1; l.pb(i+1); r.pb(tab[i].sc+1); swap(tab[i], tab[tab[i].sc]); } } reverse(r.begin(), r.end()); l.insert(l.end(), r.begin(), r.end()); all.pb(l); } cout<<all.size()<<"\n"; for (auto runda : all){ cout<<runda.size()<<"\n"; for (int p : runda){ cout<<p<<" "; } cout<<"\n"; } return 0; } |
English