C. Sorting by Subsequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
Copy
6 3 2 1 6 5 4
output
Copy
4 2 1 3 1 2 2 4 6 1 5
input
Copy
6 83 -75 -49 11 37 62
output
Copy
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.
题意:给你一个乱序的序列,让你将它划分为k个子序列并将这个子序列排序, 要求排序后的这k个子序列组成的完整序列为升序,问最大的k是多少,并将这k个序列的中的元素在原序列的下标输出;
思路:我们先保存原序列元素的下标,然后在将数组排序,将元素在原序列的下标与排完序后的新下标一一对应起来,然后我们会发现划分到一个子序列的元素的下标会构成一个环,譬如第一个样例:
3 2 1 6 5 4 ---
1 2 3 4 5 6
他的划分集合为 {1,3} ,{2},{4,6}, {5}, 而他们在原序列的下标和新序列的下标恰好构成一个环,比如{1,3} 这个集合
元素3的原序列下标为1,在新序列中第一个元素的值为1,而原序列中值为1的下标恰好为3,这就构成了一个环;
所以我们先将元素在原序列和新序列的下标对应起来,然后开始dfs,直到搜索到已经搜索过的值结束(可能有点绕,具体看代码吧)
#include<bits/stdc++.h>
using namespace std;
struct node{
int id, val, id2;
}a[100005];
map<int, int> p;
vector<int> ans[100005];
int vis[100005];
bool cmp(node a, node b){
return a.val < b.val;
}
void dfs(int l,int step)
{
if(vis[l])//搜到了已经被标记过的下标了,说明这个集合已经找到了
return ;
ans[step].push_back(l); //没被搜到过就存进去
vis[l] = 1;
if(!vis[p[l]])
dfs(p[l], step);
}
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i].val);
a[i].id = i;
}
sort(a + 1, a + n + 1, cmp);
p.clear();
for(int i = 1; i <= n; i++)
p[a[i].id] = i;//新序列和原序列进行映射,好进行dfs
memset(vis, 0, sizeof(vis));
int step = 0;
for(int i = 1; i <= n; i++)
{
if(!vis[a[i].id]){//若这个元素在原序列下标没有被搜过,就开始dfs
dfs(a[i].id ,step);
step++;//step表示第几个子序列
}
}
printf("%d\n", step);
for(int i = 0; i < step; i++)
{
printf("%d ", ans[i].size());
for(int j = 0; j < ans[i].size(); j++)
printf("%d%c", ans[i][j], j == ans[i].size() - 1? '\n' : ' ');
}
return 0;
}