ZOJ3963-Heap Partition

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
Hint

Author:  LIN, Xi
Source:  The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple


题意 : 构造尽可能少的一种结构,父节点的值小于等于子节点,而且子节点在序列中出现在父节点后面。而且每个父节点至多有两个子节点。

解题思路:线段树+离散化,判断每个数下面可以接几个儿子节点


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, visit[100050], b[100050], sum[100050 * 4];
struct node
{
	int id, x;
}ans[100050];

void build(int k, int l, int r)
{
	sum[k] = 0;
	if (l == r) return;
	int mid = (l + r) >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
}

int query(int k, int l, int r, int ll, int rr)
{
	if (sum[k] == 0) return 0;
	if (l >= ll&&r <= rr)
	{
		if (l == r) return l;
		int mid = (l + r) >> 1, ma;
		if ((ma = query(k << 1 | 1, mid + 1, r, ll, rr))) return ma;
		else return query(k << 1, l, mid, ll, rr);
	}
	else
	{
		int mid = (l + r) >> 1, ma;
		if (mid < rr && (ma = query(k << 1 | 1, mid + 1, r, ll, rr))) return ma;
		else return query(k << 1, l, mid, ll, rr);
	}
}

void add(int k, int l, int r, int x, int val)
{
	if (l == r)
	{
		sum[k] += val;
		return;
	}
	int mid = (l + r) >> 1;
	if (x <= mid) add(k << 1, l, mid, x, val);
	else add(k << 1 | 1, mid + 1, r, x, val);
	sum[k] = max(sum[k << 1], sum[k << 1 | 1]);
}

bool cmp(node a, node b)
{
	if (a.x != b.x) return a.x < b.x;
	else return a.id < b.id;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int xx, cnt = 1;
		scanf("%d%d", &n, &xx);
		for (int i = 1; i <= n; i++) visit[i] = 0;
		build(1, 1, n);
		ans[1].id = 1, ans[1].x = 1;
		visit[1]++, b[xx] = 1;
		add(1, 1, n, xx, 2);
		for (int i = 2; i <= n; i++)
		{
			scanf("%d", &xx);
			int k = query(1, 1, n, 1, xx);
			ans[i].id = i;
			k == 0 ? ans[i].x = ++cnt : ans[i].x = b[k];
			add(1, 1, n, xx, 2);
			if (k != 0) add(1, 1, n, k, -1);
			visit[ans[i].x]++, b[xx] = ans[i].x;
		}
		sort(ans + 1, ans + 1 + n, cmp);
		printf("%d\n", cnt);
		printf("%d %d", visit[1], ans[1].id);
		for (int i = 2; i <= n; i++)
		{
			if (ans[i].x == ans[i - 1].x) printf(" %d", ans[i].id);
			else printf("\n%d %d", visit[ans[i].x], ans[i].id);
		}
		printf("\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值