CodeForces - 844C Sorting by Subsequences (排序+思维)
2017-08-25 20:14
513 查看
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
Output
Input
Output
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代码:
View Code
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 160C Find Pair【思维+排序】
- Codeforces 593B Anton and Lines 【思维】【枚举】【排序】
- CodeForces - 439D Devu and his Brother 思维+排序
- Codeforces - 612D. The Union of k-Segments 排序+思维
- codeforces-749【思维】
- codeforces 894B. Ralph And His Magic Field (数学题+思维)
- CodeForces 360A - Levko and Array Recovery (思维)
- codeforces 632C (练习Java排序)
- Codeforces 817D Imbalanced Array【思维】好题!
- Codeforces 454D Little Pony and Harmony Chest【思维+状压Dp+记录路径】好题!
- Codeforces 954C - Matrix Walk(思维 + 模拟)
- Codeforces 101B Buses 排序+树状数组
- CodeForces 721D. Maxim and Array(贪心,思维题)
- codeforces 232A Cycles 图论 贪心 思维
- Codeforces-298A 思维训练
- 【Codeforces】-699A-Launch of Collider(思维)
- LeetCode 406 Queue Reconstruction by Height (排序 思维)
- CodeForces - 862E Mahmoud and Ehab and the function 二分(思维题)
- Codeforces 371B - Filya and Homework(思维)
- Codeforces 818D Multicolored Car【思维+线段树】