Heap Partition
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
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
Hint
d.构造尽可能少的最小堆,
比如样例2中,最少可构造2个,分别是2 4 3和1
输出的是数字在原序列中的位置
s.前面的数字从小到大排序,贪心选择尽可能大的构造
如果找不到比当前数字小的,则当前数字作为根,添加一个堆
样例较大,用set来写,二分查找比较快
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int MAXN = 1e5 + 5; 5 6 struct Node { 7 int id; 8 int val; 9 } a[MAXN]; 10 11 int fa[MAXN]; 12 int childNum[MAXN];// 13 14 struct NodeCmp { 15 bool operator()(const Node &a, const Node &b) 16 { 17 if (a.val != b.val) return a.val < b.val; 18 return a.id < b.id; 19 } 20 }; 21 22 set<Node, NodeCmp> st;//按val排序 23 vector<int> vt[MAXN];//保存儿子节点 24 vector<int> vt2;//保存父节点 25 26 int setFind(int d) 27 { 28 if (fa[d] < 0) { 29 return d; 30 } 31 return fa[d] = setFind(fa[d]); 32 } 33 34 void setJoin(int x, int y) 35 { 36 x = setFind(x); 37 y = setFind(y); 38 if (x != y) fa[x] = y; 39 } 40 41 int main() 42 { 43 int T; 44 int n; 45 int i, j; 46 Node tmp; 47 set<Node>::iterator it; 48 int tmp2;// 49 50 scanf("%d", &T); 51 52 while (T--) { 53 //这样初始化超时 54 //memset(fa, -1, sizeof(fa)); 55 //memset(childNum, 0, sizeof(childNum)); 56 scanf("%d", &n); 57 memset(fa, -1, sizeof(int) * (n + 1)); 58 memset(childNum, 0, sizeof(int) * (n + 1)); 59 st.clear(); 60 vt2.clear(); 61 for (i = 0; i < n; ++i) { 62 scanf("%d", &a[i].val); 63 a[i].id = i + 1; 64 it = st.upper_bound(a[i]); 65 if (it == st.begin()) {// 66 st.insert(a[i]); 67 vt2.push_back(a[i].id); 68 } else { 69 tmp = *(--it); 70 setJoin(a[i].id, tmp.id); 71 ++childNum[tmp.id]; 72 if (childNum[tmp.id] >= 2) { 73 st.erase(tmp); 74 } 75 76 vt[setFind(tmp.id)].push_back(a[i].id);//加到根节点孩子列表 77 st.insert(a[i]); 78 } 79 } 80 81 printf("%d\n", vt2.size()); 82 for (i = 0; i < vt2.size(); ++i) { 83 tmp2 = vt2[i];//根节点 84 printf("%d", vt[tmp2].size()); 85 printf(" %d", tmp2);//根节点 86 for (j = 0; j < vt[tmp2].size(); ++j) {//孩子节点 87 printf(" %d", vt[tmp2][j]); 88 } 89 vt[tmp2].clear();//在这里清空比较好 90 } 91 } 92 93 return 0; 94 }