#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int,int> pii;
typedef pair<string,string> pss;
const int M=3e3+3;
int wej[M];
pii tab[M];
int wyb[M];
int n;
vector<vector<int>> wp(M);
vector<vector<int>> wk(M);
int bin (int w)
{
int p=0,k=n;
while(tab[(p+k)/2].second!=w)
{
if(tab[(p+k)/2].second<w)p=((p+k)/2)+1;
else k=((p+k)/2)-1;
}
return (p+k)/2;
}
bool cmp(pii a,pii b)
{
return a.second<b.second;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
pss a;
int p=0,zro=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>wej[i];
tab[i].second=wej[i];
tab[i].first=i;
}
sort(tab+1,tab+n+1,cmp);
zro=1;
int ilo=0;
while(zro!=0)
{
p++;
zro=0;
ilo=0;
for(int i=1;i<=n;i++)
{
if(tab[i].second!=wej[i] && wyb[i]!=p && wyb[tab[i].first]!=p && ilo<=n/2)
{
wyb[i]=p;
wyb[tab[i].first]=p;
wp[p].push_back(i);
wk[p].push_back(tab[i].first);
//cout<<i<<' '<<tab[i].first<<' ';
tab[bin(wej[i])].first=tab[i].first;
swap(wej[i],wej[tab[i].first]);
tab[i].first=i;
zro++;
ilo++;
}
}
/*for(int i=1;i<=n;i++)
{
cout<<wej[i]<<' ';
}*/
//cout<<'\n';
}
cout<<p-1<<'\n';
for(int i=1;i<p;i++)
{
//cout<<wp[1][1]<<' ';
cout<<2*wp[i].size()<<'\n';
for(int j=0;j<wp[i].size();j++)
{
cout<<wp[i][j]<<' ';
}
for(int j=wp[i].size()-1;j>=0;j--)
{
cout<<wk[i][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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef pair<int,int> pii; typedef pair<string,string> pss; const int M=3e3+3; int wej[M]; pii tab[M]; int wyb[M]; int n; vector<vector<int>> wp(M); vector<vector<int>> wk(M); int bin (int w) { int p=0,k=n; while(tab[(p+k)/2].second!=w) { if(tab[(p+k)/2].second<w)p=((p+k)/2)+1; else k=((p+k)/2)-1; } return (p+k)/2; } bool cmp(pii a,pii b) { return a.second<b.second; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); pss a; int p=0,zro=0; cin>>n; for(int i=1;i<=n;i++) { cin>>wej[i]; tab[i].second=wej[i]; tab[i].first=i; } sort(tab+1,tab+n+1,cmp); zro=1; int ilo=0; while(zro!=0) { p++; zro=0; ilo=0; for(int i=1;i<=n;i++) { if(tab[i].second!=wej[i] && wyb[i]!=p && wyb[tab[i].first]!=p && ilo<=n/2) { wyb[i]=p; wyb[tab[i].first]=p; wp[p].push_back(i); wk[p].push_back(tab[i].first); //cout<<i<<' '<<tab[i].first<<' '; tab[bin(wej[i])].first=tab[i].first; swap(wej[i],wej[tab[i].first]); tab[i].first=i; zro++; ilo++; } } /*for(int i=1;i<=n;i++) { cout<<wej[i]<<' '; }*/ //cout<<'\n'; } cout<<p-1<<'\n'; for(int i=1;i<p;i++) { //cout<<wp[1][1]<<' '; cout<<2*wp[i].size()<<'\n'; for(int j=0;j<wp[i].size();j++) { cout<<wp[i][j]<<' '; } for(int j=wp[i].size()-1;j>=0;j--) { cout<<wk[i][j]<<' '; } cout<<'\n'; } } |
English