ZOJ3963-Heap Partition
来源:互联网 发布:炒股记账软件 编辑:程序博客网 时间:2024/06/01 20:25
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
441 2 3 442 4 3 141 1 1 153 2 1 4 1
Sample Output
14 1 2 3 423 1 2 31 414 1 2 3 432 1 41 22 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 longconst 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;}
- ZOJ3963 Heap Partition
- ZOJ3963-Heap Partition
- ZOJ3963 贪心
- ZOJ 3963 Heap Partition (STL)
- ZOJ 3963 Heap Partition(贪心)
- ZOJ 3963 Heap Partition(贪心)
- zoj 3963 Heap Partition 贪心+upper_bound()
- 浙江14届省赛 F.Heap Partition(ZOJ 3963)[贪心][二分]
- ZOJ 3963 Heap Partition( 并查集 + 贪心 +二分 )
- ZOJ-3963-Heap Partition(贪心)(STL)
- ZOJ3963 【gay gay 的贪心】
- Partition
- PARTITION
- partition
- partition
- partition
- Partition
- partition
- Apache ActiveMQ的优缺点
- Warning: usage of --preserve-metadata with option "resource-rules" (deprecated in Mac OS X >= 10.10)
- 已知三点求平面方程、平面法向量和点到平面的距离(转载)
- 洛谷P3384【模板】树链剖分 (树链剖分)
- matplotlib Artist 教程
- ZOJ3963-Heap Partition
- Java总结篇系列:Java多线程(一)
- 红黑树 删除某节点后 旋转3次 举例
- 【MongoDB for Java】Java操作MongoDB
- vue.js:[Vue warn]: Cannot find element:
- div模拟模态对话框
- vert.x:compose
- effective--构建器
- 图层遮盖式进度条