Heap Partition ZOJ - 3963

本文介绍了一种算法,用于将一个序列分解为最小数量的堆序列子序列。通过使用离散化、并查集和STL等技术手段,该算法能够有效地解决此问题,并给出具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, sjsi 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 ≤ ain).

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
#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值