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.
Output
For 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 Input
4 4 1 2 3 4 4 2 4 3 1 4 1 1 1 1 5 3 2 1 4 1
Sample 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
题意:给你一个序列 问最少需要构造几颗二叉树 使得对于一个不为根的点 他的下标大于父亲的下标 值大于等于父亲的值
题解:找到小于等于当前结点的结点 然后插进去 如果没有 就重新造一棵树 用set搞一搞就行
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
struct node{
int num,lab;
bool operator <(const node& a)const{
if(num==a.num)return lab<a.lab;
return num<a.num;
}
};
set<node>sp;
set<node>::iterator it;
vector<int>sps[100005];
int yes[100005],vis[100005],cnt;
int main(){
int t,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
sp.clear();
cnt=0;
int x;
node f;
for(i=1;i<=n;i++){
scanf("%d",&x);
f.num=x;
f.lab=i;
yes[i]=0;
sp.insert(f);
node l=*sp.begin();
if(l.num==f.num&&l.lab==f.lab){
cnt++;
sps[cnt].clear();
sps[cnt].push_back(i);
vis[i]=cnt;
}
else{
it=sp.find(f);
it--;
l=*it;
yes[l.lab]++;
vis[i]=vis[l.lab];
sps[vis[i]].push_back(i);
if(yes[l.lab]==2)sp.erase(it);
}
}
printf("%d\n",cnt);
for(i=1;i<=cnt;i++){
int d=sps[i].size();
printf("%d",d);
for(j=0;j<d;j++){
printf(" %d",sps[i][j]);
}
printf("\n");
}
}
return 0;
}