#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<pll> vll;
typedef vector<pii> vii;
typedef double db;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define st first
#define nd second
#define FOR(i,s,k) for(auto i=s; i<k; i++)
#define ROF(i,s,k) for(auto i=k-1; i>=s; i--)
constexpr ll inf = 1e18+1;
constexpr ll MAXN = 1e6+5;
constexpr ll LOGN = 18;
void skaluj(vi& V)
{
vi W;
FOR(i,1,sz(V)) W.pb(V[i]);
sort(all(W));
int n = 1;
map<int,int> M;
for(auto& e:W) M[e] = n++;
FOR(i,1,sz(V)) V[i] = M[V[i]];
}
bool okej(vi& V)
{
FOR(i,1,sz(V)) if(V[i]!=i) return false;
return true;
}
int main()
{
BOOST;
int n;
cin>>n;
vi V(n+1);
vi P(n+1);
FOR(i,1,n+1)
cin>>V[i];
skaluj(V);
FOR(i,1,n+1) P[V[i]] = i;//poprzednik tak jakby
vector<vi> ans;
while(!okej(V))
{
bitset<MAXN> B;
vi pom1, pom2;
FOR(i,1,n+1) if(!B[i] && V[i]!=i)
{
int u = i; //będę nim szedł do tyłu
int v = V[V[u]];//będę nim szedł do przodu
if(u==v) //cykl wielkości = 2
{
v=V[u];
B[u] = B[v] = 1;
pom1.pb(u);
pom2.pb(v);
}
else
B[V[u]] = 1;
while(!B[u] && !B[v] && u!=v)
{
B[u] = B[v] = 1;
pom1.pb(u);
pom2.pb(v);
u = P[u];
v = V[v];
}
}
ans.pb({});
FOR(i,0,sz(pom1)) ans.back().pb(pom1[i]);
ROF(i,0,sz(pom2)) ans.back().pb(pom2[i]);
FOR(i,0,sz(ans.back())/2)
swap(V[ans.back()[i]], V[ ans.back()[ sz(ans.back())-1-i ] ]);
FOR(i,1,n+1) P[V[i]] = i;
}
cout<<sz(ans)<<"\n";
FOR(i,0,sz(ans))
{
cout<<sz(ans[i])<<"\n";
FOR(j,0,sz(ans[i])) cout<<ans[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 87 88 89 90 91 92 93 94 95 96 97 98 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pll; typedef pair<int,int> pii; typedef vector<ll> vl; typedef vector<int> vi; typedef vector<pll> vll; typedef vector<pii> vii; typedef double db; #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define sz(x) (int)x.size() #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define st first #define nd second #define FOR(i,s,k) for(auto i=s; i<k; i++) #define ROF(i,s,k) for(auto i=k-1; i>=s; i--) constexpr ll inf = 1e18+1; constexpr ll MAXN = 1e6+5; constexpr ll LOGN = 18; void skaluj(vi& V) { vi W; FOR(i,1,sz(V)) W.pb(V[i]); sort(all(W)); int n = 1; map<int,int> M; for(auto& e:W) M[e] = n++; FOR(i,1,sz(V)) V[i] = M[V[i]]; } bool okej(vi& V) { FOR(i,1,sz(V)) if(V[i]!=i) return false; return true; } int main() { BOOST; int n; cin>>n; vi V(n+1); vi P(n+1); FOR(i,1,n+1) cin>>V[i]; skaluj(V); FOR(i,1,n+1) P[V[i]] = i;//poprzednik tak jakby vector<vi> ans; while(!okej(V)) { bitset<MAXN> B; vi pom1, pom2; FOR(i,1,n+1) if(!B[i] && V[i]!=i) { int u = i; //będę nim szedł do tyłu int v = V[V[u]];//będę nim szedł do przodu if(u==v) //cykl wielkości = 2 { v=V[u]; B[u] = B[v] = 1; pom1.pb(u); pom2.pb(v); } else B[V[u]] = 1; while(!B[u] && !B[v] && u!=v) { B[u] = B[v] = 1; pom1.pb(u); pom2.pb(v); u = P[u]; v = V[v]; } } ans.pb({}); FOR(i,0,sz(pom1)) ans.back().pb(pom1[i]); ROF(i,0,sz(pom2)) ans.back().pb(pom2[i]); FOR(i,0,sz(ans.back())/2) swap(V[ans.back()[i]], V[ ans.back()[ sz(ans.back())-1-i ] ]); FOR(i,1,n+1) P[V[i]] = i; } cout<<sz(ans)<<"\n"; FOR(i,0,sz(ans)) { cout<<sz(ans[i])<<"\n"; FOR(j,0,sz(ans[i])) cout<<ans[i][j]<<" "; cout<<"\n"; } } |
English