# BZOJ 1078: [SCOI2008]斜堆

## 1078: [SCOI2008]斜堆

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 770  Solved: 422
[Submit][Status][Discuss]

## Description

斜堆(skew heap)是一种常用的数据结构。它也是二叉树，且满足与二叉堆相同的堆性质：每个非根结点的值

## Input

第一行包含一个整数n。第二行包含n个整数d1, d2, ... , dn， di < 100表示i是di的左儿子，di>=100表示i

## Output

仅一行，包含n+1整数，即字典序最小的插入序列。

## Sample Input

6
100 0 101 102 1 2

0 1 2 3 4 5 6

## Source

[Submit][Status][Discuss]

  1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 #include <iostream>
5 #include <algorithm>
6
7 #define siz 1024
8
9 inline int get_c(void)
10 {
11     static char buf[siz];
12     static char *head = buf + siz;
13     static char *tail = buf + siz;
14
17
19 }
20
21 inline int get_i(void)
22 {
23     register int ret = 0;
24     register int neg = false;
25     register int bit = get_c();
26
27     for (; bit < 48; bit = get_c())
28         if (bit == '-')neg ^= true;
29
30     for (; bit > 47; bit = get_c())
31         ret = ret * 10 + bit - 48;
32
33     return neg ? -ret : ret;
34 }
35
36 #define maxn 205
37
38 int n, ans[maxn];
39
40 struct node
41 {
42     node *lson;
43     node *rson;
44     node *father;
45
46     node(void)
47     {
48         lson = NULL;
49         rson = NULL;
50         father = NULL;
51     }
52
53     inline void swap(void)
54     {
55         static node *temp;
56
57         temp = lson;
58         lson = rson;
59         rson = temp;
60     }
61 }tree[maxn], *root = tree;
62
63 inline int last(void)
64 {
65     node *t = root;
66
67     while (t->rson)
68         t = t->lson;
69
70     if (t->lson && !t->lson->lson)
71         t = t->lson;
72
73     if (t == root)
74         root = t->lson;
75     else
76         t->father->lson = t->lson;
77
78     if (t->lson)
79         t->lson->father = t->father;
80
81     for (node *p = t->father; p; p = p->father)
82         p->swap();
83
84     return int(t - tree);
85 }
86
87 signed main(void)
88 {
89     n = get_i();
90
91     for (int i = 1; i <= n; ++i)
92     {
93         int fa = get_i();
94
95         if (fa < 100)
96             tree[i].father = tree + fa, tree[fa].lson = tree + i;
97         else fa -= 100,
98             tree[i].father = tree + fa, tree[fa].rson = tree + i;
99     }
100
101     for (int i = n; i >= 0; --i)ans[i] = last();
102
103     for (int i = 0; i <= n; ++i)printf("%d ", ans[i]);
104
105     //system("pause");
106 }

@Author: YouSiki

posted @ 2016-12-25 10:58  YouSiki  阅读(327)  评论(0编辑  收藏  举报