码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-25 21:01:04      阅读:780      评论:0      收藏:0      [点我收藏+]

标签:scan   opened   lag   dice   bit   turn   打印   out   排序   

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.

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.

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

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.

Output

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

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.

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

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

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:

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

 

 

 

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

标签:scan   opened   lag   dice   bit   turn   打印   out   排序   

原文地址:http://www.cnblogs.com/wang-ya-wei/p/7429535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!