ZOJ 3963 构造

Heap Partition

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

A sequence S = {s1s2, ..., 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 sjsj ≤ 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 a1a2, ..., 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 a1a2, ..., 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 Pi1Pi2, ..., 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;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值