代码拉取完成,页面将自动刷新
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
struct Heap
{
Heap *lson, *rson;
int dist, val;
Heap()
{
lson = rson = NULL;
dist =val = 0;
}
}*root = NULL;
void set_dist(Heap *node)
{
if (node->rson != NULL)
node->dist = node->rson->dist + 1;
else node->dist = 0;
}
Heap *merge(Heap *a, Heap *b)
{
//一个节点是空的,那么返回另一个节点
if (a == NULL) return b;
else if (b == NULL) return a;
//将权值较小的作为根结点
if (a->val > b->val) swap(a, b);
//合并右儿子和大权值节点
a->rson = merge(a->rson, b);
if (a->rson != NULL && a->lson != NULL)
{
if (a->lson->dist < a->rson->dist)
swap(a->lson, a->rson);
}
else if (a->lson == NULL && a->rson != NULL)
swap(a->lson, a->rson);
set_dist(a);
return a;
}
void Insert(int val)
{
//必须先特判堆是空的结果
if (root == NULL)
{
root = new Heap();
root->val = val;
set_dist(root);
return ;
}
Heap *num = new Heap();
num->val = val;
set_dist(num);
root = merge(root, num);
}
int Top()
{
if (root != NULL)
return root->val;
else return 0; //rand();
}
void Pop()
{
Heap *temp = root;
root = merge(root->lson, root->rson);
delete(temp);
}
int main()
{
int n, opt, val;
cin >> n;
while (n--)
{
cin>>opt;
switch (opt)
{
case 1:cin>>val;Insert(val);break;
case 2:cout<<Top()<<endl;break;
case 3:Pop();break;
default:break;
}
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。