我是靠谱客的博主 勤恳电话,最近开发中收集的这篇文章主要介绍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("%dn", 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;
}

最后

以上就是勤恳电话为你收集整理的ZOJ3963-Heap Partition的全部内容,希望文章能够帮你解决ZOJ3963-Heap Partition所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部