CodeForces - 844C Sorting by Subsequences (排序+思惟)

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 that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.node

Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.ios

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

Input

The first line of input data contains integer n (1 ≤ n ≤ 105) — the length of the sequence.數組

The second line of input data contains n different integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.app

Output

In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.ide

In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci ≤ n), then ci integers l1, l2, ..., lci (1 ≤ lj ≤ n) — indices of these elements in the original sequence. ui

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

If there are several possible answers, print any of them.spa

Example
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note

In the first sample output:code

After sorting the first subsequence we will get sequence 1 2 3 6 5 4.

Sorting the second subsequence changes nothing.

After sorting the third subsequence we will get sequence 1 2 3 4 5 6.

Sorting the last subsequence changes nothing.

 

題目大意:給你一個亂序的序列,把這些序列中的放入1-k 的集合中,集合具備排序功能,最後將集合1-k中的數依次打印出來是排好序的。問k的最大值是多少?每一個集合的大小和集合中的值分別是多少。

 

解題思路:對於每個值都會有排序值後應該在的位置,咱們用dfs不停的去找該位置上的值排序以後應該在的位置,對於找過的位置進行標記,把序列中的全部位置dfs一遍就獲得了集合中的值。而後這些值咱們應該放在優先隊列中,省去了排序的環節。其中優先隊列應該定義爲數組的形式,這樣就可以找出k了。如不理解,請看代碼:

 

AC代碼:

 1 #include <iostream>
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int a[100005];
 5 struct node
 6 {
 7     int val,id,flag;
 8 } b[100005];
 9 priority_queue<int ,vector<int>,greater<int> > que[100005];
10 int cmp(const node &a,const node &b)
11 {
12     return a.val<b.val;
13 }
14 int dfs(int id,int k)
15 {
16     que[k].push(id);
17     if(b[id].flag==0) return 0;
18     else
19     {
20         b[id].flag=0;
21         dfs(b[id].id,k);
22     }
23     return 0;
24 }
25 int main()
26 {
27     int n;
28     while(~scanf("%d",&n))
29     {
30         for(int i=1; i<=n; i++)
31         {
32             scanf("%d",&b[i].val);
33             b[i].id=i;
34             b[i].flag=1;
35         }
36         sort(b+1,b+n+1,cmp);
37         int k=0;
38         for(int i=1;i<=n;i++)
39         {
40             if(b[i].flag)
41             {
42                 b[i].flag=0;
43                 dfs(b[i].id,k);
44                 k++;
45             }
46         }
47         printf("%d\n",k);
48         for(int i=0;i<k;i++)
49         {
50             printf("%d",que[i].size());
51             while(!que[i].empty())
52             {
53                 printf(" %d",que[i].top());
54                 que[i].pop();
55             }
56             printf("\n");
57         }
58     }
59     return 0;
60 }
View Code
相關文章
相關標籤/搜索