#include <bits/stdc++.h>
//#include <fstream>
using namespace std;
int pls,dl,id,a,n,wyn,o[3007],powe,akt;
vector<int>sk,t,ckl[3007],poc,kon;
int mp[3007];
void dfs(int w)
{
ckl[id].push_back(w);
dl++;
o[w]=1;
if(o[t[w]]==0)
{
dfs(t[w]);
}
}
int main()
{
ios_base::sync_with_stdio(NULL);
cin.tie();
cout.tie();
//fstream plik;
//plik.open("sus.txt", ios::out);
cin>>n;
t.push_back(0);
for(int i=0; i<n; i++)
{
cin>>a;
sk.push_back(a);
t.push_back(a);
}
sort(sk.begin(), sk.end());
for(int i=0; i<n; i++)
{
mp[sk[i]]=i+1;
}
for(int i=1; i<=n; i++)
{
t[i]=mp[t[i]];
}
for(int i=1; i<=n; i++)
{
if(o[i]==0)
{
id++;
dl=0;
dfs(i);
wyn=max(dl,wyn);
}
}
dl=0;
while(wyn>1)
{
if(wyn%2==1)
{
pls=1;
}
wyn/=2;
dl++;
}
cout<<dl+pls<<"\n";
powe=2;
for(int i=0; i<dl+pls; i++)
{
poc.clear();
kon.clear();
for(int j=1; j<=id; j++)
{
if(powe/2<int(ckl[j].size()))
{
akt=1;
while(akt+powe/2<=int(ckl[j].size()))
{
poc.push_back(ckl[j][akt-1]);
kon.push_back(ckl[j][akt+powe/2-1]);
akt+=powe;
}
}
}
cout<<poc.size()+kon.size()<<"\n";
for(int i=0; i<int(poc.size()); i++)
{
cout<<poc[i]<<" ";
}
for(int i=0; i<int(kon.size()); i++)
{
cout<<kon[kon.size()-i-1]<<" ";
}
cout<<"\n";
powe*=2;
}
//plik.close();
}
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 | #include <bits/stdc++.h> //#include <fstream> using namespace std; int pls,dl,id,a,n,wyn,o[3007],powe,akt; vector<int>sk,t,ckl[3007],poc,kon; int mp[3007]; void dfs(int w) { ckl[id].push_back(w); dl++; o[w]=1; if(o[t[w]]==0) { dfs(t[w]); } } int main() { ios_base::sync_with_stdio(NULL); cin.tie(); cout.tie(); //fstream plik; //plik.open("sus.txt", ios::out); cin>>n; t.push_back(0); for(int i=0; i<n; i++) { cin>>a; sk.push_back(a); t.push_back(a); } sort(sk.begin(), sk.end()); for(int i=0; i<n; i++) { mp[sk[i]]=i+1; } for(int i=1; i<=n; i++) { t[i]=mp[t[i]]; } for(int i=1; i<=n; i++) { if(o[i]==0) { id++; dl=0; dfs(i); wyn=max(dl,wyn); } } dl=0; while(wyn>1) { if(wyn%2==1) { pls=1; } wyn/=2; dl++; } cout<<dl+pls<<"\n"; powe=2; for(int i=0; i<dl+pls; i++) { poc.clear(); kon.clear(); for(int j=1; j<=id; j++) { if(powe/2<int(ckl[j].size())) { akt=1; while(akt+powe/2<=int(ckl[j].size())) { poc.push_back(ckl[j][akt-1]); kon.push_back(ckl[j][akt+powe/2-1]); akt+=powe; } } } cout<<poc.size()+kon.size()<<"\n"; for(int i=0; i<int(poc.size()); i++) { cout<<poc[i]<<" "; } for(int i=0; i<int(kon.size()); i++) { cout<<kon[kon.size()-i-1]<<" "; } cout<<"\n"; powe*=2; } //plik.close(); } |
English