A sequence S = {s1, s2, ..., sn} is called heapable if there exists a binary tree T with n nodes such that every node is labelled with exactly one element from the sequence S, and for every non-root node si and its parent sj, sj ≤ si and j < i hold. Each element in sequence S can be used to label a node in tree T only once.
Chiaki has a sequence a1, a2, ..., an, she would like to decompose it into a minimum number of heapable subsequences.
Note that a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contain an integer n (1 ≤ n ≤ 105) — the length of the sequence.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
It is guaranteed that the sum of all n does not exceed 2 × 106.
OutputFor each test case, output an integer m denoting the minimum number of heapable subsequences in the first line. For the next m lines, first output an integer Ci, indicating the length of the subsequence. Then output Ci integers Pi1, Pi2, ..., PiCi in increasing order on the same line, where Pij means the index of the j-th element of the i-th subsequence in the original sequence.
Sample Input4 4 1 2 3 4 4 2 4 3 1 4 1 1 1 1 5 3 2 1 4 1Sample Output
1 4 1 2 3 4 2 3 1 2 3 1 4 1 4 1 2 3 4 3 2 1 4 1 2 2 3 5
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
typedef long long ll;
using namespace std;
/*
利用map,神奇的自动排序功能,进行二分,删除
利用并查集保存关系,这样就不会乱了,神奇的stl
*/
const int maxn=1e5+10;
int vis[maxn];
int pre[maxn],is_ful[maxn];
set<int> s;
vector<int> vec[maxn];
map<int,int> mp;
struct node{
int val,st_rk,ed_rk;
}p1[maxn];
int cmp_val(node a,node b){
if(a.val==b.val)
return a.st_rk<b.st_rk;
return a.val<b.val;
}
int cmp_st_rk(node a,node b){
return a.st_rk<b.st_rk;
}
int find_rt(int x){
return pre[x]==x?x:pre[x]=find_rt(pre[x]);
}
void join(int a,int b){
int ra=find_rt(a),rb=find_rt(b);
if(ra!=rb){
pre[ra]=rb;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&p1[i].val);
p1[i].st_rk=i;
pre[i]=i,vis[i]=is_ful[i]=0;
vec[i].clear();
}
s.clear();
mp.clear();
sort(p1+1,p1+n+1,cmp_val);
for(int i=1;i<=n;i++){//离散化
p1[i].ed_rk=i;
}
sort(p1+1,p1+n+1,cmp_st_rk);
for(int i=1;i<=n;i++){
set<int>::iterator it=s.upper_bound(p1[i].ed_rk);
if(it==s.begin()||*(s.begin())>p1[i].ed_rk){
s.insert(p1[i].ed_rk);
}
else{
it--;
is_ful[*it]++;
join(*it,p1[i].ed_rk);
if(is_ful[*it]>=2){
s.erase(*it);
}
s.insert(p1[i].ed_rk);
}
}
int cnt=0;
for(int i=1;i<=n;i++){
int rt=find_rt(p1[i].ed_rk);
if(!vis[rt]){
vis[rt]=1;
mp[rt]=cnt++;
}
int id=mp[rt];
vec[id].push_back(i);
}
printf("%d\n",cnt);
for(int i=0;i<cnt;i++){
int sz=vec[i].size();
printf("%d",sz);
for(int j=0;j<sz;j++){
printf(" %d",vec[i][j]);
}
vec[i].clear();
printf("\n");
}
}
return 0;
}