Codeforces 844 C. Sorting by subsequences (Circular section) _ Common skills

Source: Internet
Author: User
Description

You are given a sequence A1, A2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such, after sorting integers in each of them in increasing order, the total sequence also would be sorted in increasing order.

sorting integers in a subsequence are a process such that numbers included in a subsequence are ordered in increasing O Rder, and the numbers which are not included in a subsequence don ' t change their places.

Every element of the sequence must appear in exactly one subsequence.

Input

The ' a ' of input data contains integer n (1≤n≤10^5)-the length of the sequence.

The second line of input data contains n different integers a1, A2, ..., an (-10^9≤ai≤10^9)-the elements of the sequ ence. It is guaranteed this all elements of the sequence are distinct.

Output

In the maximum number of subsequences K, which the original sequence can is split into while Fulfilli ng the requirements.

In the next K lines print the description of subsequences to following format:the number of elements in subsequence C I (0 < ci≤n), then ci integers L1, L2, ..., LCI (1≤lj≤n)-indices of these elements in the original sequence.

Indices could is printed in the any order. Every index from 1 to n must appear in output exactly once.

If There are several possible answers, print any of them.

examples Input

6
3 2 1 6 5 4

examples Output

4
2 1 3
1 2
2 4 6 1 5

the

The number of the maximal subsequence of the original sequence is found, and the original sequence can be ordered after the inner ordering of each subsequence is satisfied.

train of Thought

For each number of the original sequence, look for the permutation of the order after the cycle section can be.

AC Code

#include <bits/stdc++.h> #define IO Ios::sync_with_stdio (false); \ cin.tie (0); \ cout.tie (0);
using namespace Std;
const int MAXN = 1E5+10;

typedef __int64 LL;
    struct node {int id;
int num;

} A[MAXN];
int N,tot;
BOOL VIS[MAXN];

Vector<int> ANS[MAXN];
        void Solve () {for (int i=0; i<n; i++) {if (vis[i)) continue;
        Vis[i] = true;
        Ans[tot].push_back (i);
        int now = a[i].id;
            while (!vis[now]) {ans[tot].push_back (now);
            Vis[now] = true;
        now = a[now].id;
    } tot++;
    } cout<<tot<<endl;
        for (int i=0; i<tot; i++) {cout<<ans[i].size () << "";
        Sort (Ans[i].begin (), Ans[i].end ());
        for (auto s:ans[i]) cout<<s+1<< "";
    cout<<endl;
    int main () {IO;
    cin>>n;
        for (int i=0; i<n; i++) {cin>>a[i].num; A[i].id = i;
    Sort (a,a+n,[] (const node &x,const node &y) {return x.num<y.num;
    });
    Solve ();
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.