HDU 5818 Joint Stacks(左偏树)

Joint Stacks

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1380    Accepted Submission(s): 659


Problem Description
A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another word, the operations perform in a Last-In-First-Out (LIFO) manner.
A mergeable stack is a stack with "merge" operation. There are three kinds of operation as follows:

- push A x: insert x into stack A
- pop A: remove the top element of stack A
- merge A B: merge stack A and B

After an operation "merge A B", stack A will obtain all elements that A and B contained before, and B will become empty. The elements in the new stack are rearranged according to the time when they were pushed, just like repeating their "push" operations in one stack. See the sample input/output for further explanation.
Given two mergeable stacks A and B, implement operations mentioned above.


Input
There are multiple test cases. For each case, the first line contains an integer N(0<N≤105), indicating the number of operations. The next N lines, each contain an instruction "push", "pop" or "merge". The elements of stacks are 32-bit integers. Both A and B are empty initially, and it is guaranteed that "pop" operation would not be performed to an empty stack. N = 0 indicates the end of input.


Output
For each case, print a line "Case #t:", where t is the case number (starting from 1). For each "pop" operation, output the element that is popped, in a single line.


Sample Input
4
push A 1
push A 2
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge A B
pop A
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge B A
pop B
pop B
pop B 
0


Sample Output
Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0


Author
SYSU


Source
2016 Multi-University Training Contest 7
这道题实现的是A,B两个栈的三种操作,分别是push,pop和合并。
实现两个栈的合并的方法就是左偏树啦,能在O(nlogn)的时间复杂度下实现两个大顶堆的合并。
左偏树算法操作:
键值是用于比较节点的大小
距离则是如下定义的:节点i称为外节点,当且仅当节点i的左子树或右子树为空;节点i的距离时节点i到他的后代中,最近的外节点所经过的边数。特别地,如果节点i本身是外节点,则它的距离为0;而空节点的距离规定为-1。
左偏树的4条性质:
(1)节点的键值小于或等于它的左右子节点的键值
(2)节点的左子节点的距离不小于右子节点的距离
(3)节点的距离等于它的右子节点的距离加1
(4)一颗N个节点的左偏树距离最多为log(N+1)-1
(1)左偏树的合并
c <- Merge(A,B)
Merge()把A、B两颗左偏树合并,返回一颗新的左偏树C,包含A和B中的所有元素。这里一颗左偏树用它的根节点指针表示。
在合并操作中,最简单的情况是其中一棵树为NULL,这时只需要返回另一棵树。
若A和B都非空,假设A的根节点小于等于B的根节点(否则交换A、B),把A的根节点作为新树C的根节点,剩下的事就是合并A的右子树和B了
合并Right(A)和B之后,right(A)的距离可能会变大,当right(A)的距离大于left(A)的距离时,左偏树的性质(2)会被破坏,在这时,只需要交换left(A)和right(B);
最后,由于right(A)的距离可能发生改变,必须更新A的距离。
(2)插入新节点
单节点的树一定是左偏树,因此向左偏树插入一个节点可以看做是对两颗左偏树的合并。
(3)删除节点
删除根节点后,只需要把左子树和右子树合并。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值