#include <bits/stdc++.h>
using namespace std;
int n;
struct wzrost{
int val;
int ind;
};
wzrost h[3010];
bool warunek(wzrost A,wzrost B)
{
return A.val<B.val;
}
deque <int> wynik[3];
bool czy[3010];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>h[i].val;
h[i].ind=i;
}
sort(h+1,h+n+1,warunek);
for(int i=1;i<=n;i++)h[i].val=i;
for(int i=1;i<=n;i++)swap(h[i].val,h[i].ind);
sort(h+1,h+n+1,warunek);
for(int i=1;i<=n;i++)swap(h[i].val,h[i].ind);
for(int i=1;i<=n;i++)
{
if(czy[i]==0)
{
vector <int> q;
int a=i;
while(czy[h[a].val]==0)
{
q.push_back(h[a].val);
a=h[a].val;
czy[a]=1;
}
if(q.size()==1)continue;
if(q.size()==2)
{
wynik[1].push_back(q[0]);
wynik[1].push_front(q[1]);
}
else
{
int c=0;
int d=q.size()-2;
while(c<d)
{
wynik[1].push_back(q[c]);
wynik[1].push_front(q[d]);
c++;
d--;
}
c=0;
d=q.size()-1;
while(c<d)
{
wynik[2].push_back(q[c]);
wynik[2].push_front(q[d]);
c++;
d--;
}
}
}
}
if(wynik[1].size()==0)
{
cout<<"0";
return 0;
}
if(wynik[2].size()==0)
{
cout<<"1"<<endl;
cout<<wynik[1].size()<<endl;
for(auto x:wynik[1])cout<<x<<" ";
return 0;
}
cout<<"2"<<endl;
cout<<wynik[1].size()<<endl;
for(auto x:wynik[1])cout<<x<<" ";
cout<<endl<<wynik[2].size()<<endl;
for(auto x:wynik[2])cout<<x<<" ";
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 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 | #include <bits/stdc++.h> using namespace std; int n; struct wzrost{ int val; int ind; }; wzrost h[3010]; bool warunek(wzrost A,wzrost B) { return A.val<B.val; } deque <int> wynik[3]; bool czy[3010]; int main() { cin>>n; for(int i=1;i<=n;i++) { cin>>h[i].val; h[i].ind=i; } sort(h+1,h+n+1,warunek); for(int i=1;i<=n;i++)h[i].val=i; for(int i=1;i<=n;i++)swap(h[i].val,h[i].ind); sort(h+1,h+n+1,warunek); for(int i=1;i<=n;i++)swap(h[i].val,h[i].ind); for(int i=1;i<=n;i++) { if(czy[i]==0) { vector <int> q; int a=i; while(czy[h[a].val]==0) { q.push_back(h[a].val); a=h[a].val; czy[a]=1; } if(q.size()==1)continue; if(q.size()==2) { wynik[1].push_back(q[0]); wynik[1].push_front(q[1]); } else { int c=0; int d=q.size()-2; while(c<d) { wynik[1].push_back(q[c]); wynik[1].push_front(q[d]); c++; d--; } c=0; d=q.size()-1; while(c<d) { wynik[2].push_back(q[c]); wynik[2].push_front(q[d]); c++; d--; } } } } if(wynik[1].size()==0) { cout<<"0"; return 0; } if(wynik[2].size()==0) { cout<<"1"<<endl; cout<<wynik[1].size()<<endl; for(auto x:wynik[1])cout<<x<<" "; return 0; } cout<<"2"<<endl; cout<<wynik[1].size()<<endl; for(auto x:wynik[1])cout<<x<<" "; cout<<endl<<wynik[2].size()<<endl; for(auto x:wynik[2])cout<<x<<" "; return 0; } |
English