Pairing Heap模板

本文介绍了一种基于可并堆的数据结构实现方法,并通过一个具体的问题实例展示了其使用过程。该实现涉及C++编程语言中关键的数据结构操作,如合并、插入、删除等,并附带了一个完整的代码示例。
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1000010;
struct Node {
    int key;
    Node *l, *r;    
    inline void sc(Node *c);
} Tnull, *null = &Tnull, *root = null;
Node mem[N], *C = mem;

inline Node* newNode(int v) {
    C->key = v;
    C->l = C->r = null;
    return C++;
}

inline void Node::sc(Node *c) {
//    if (l != null)
c->r = l; l = c; } Node* merge(Node *A, Node *B) { if (A == null) return B; if (B == null) return A; if (A->key < B->key) { A->sc(B); return A; } else { B->sc(A); return B; } return null; } inline int top(Node *t) { return t->key; } Node* push(Node *t, int key) { return merge(newNode(key), t); } Node* merge(Node *t) { if (t == null || t->r == null) return t; Node *A = t, *B = t->r, *New = t->r->r; A->r = B->r = null; return merge(merge(A, B), merge(New)); } Node* pop(Node *t) { return twoPassMerge(t->l); } inline void read(int &x) { static char buf = getchar(); x = 0; for (; !isdigit(buf); buf = getchar()); for (; isdigit(buf); buf = getchar()) x = x * 10 + buf - '0'; } int main() { int n; read(n); for (int t, i = 0; i < n; ++i) read(t), root = push(root, t); for (int i = 0; i < n; ++i) { printf("%d ", top(root)); root = pop(root); } return 0; }

顺便这道题http://cogs.pro/cogs/problem/problem.php?pid=279,我调了一上午+半个下午,居然是因为可并堆打错了。。。

转载于:https://www.cnblogs.com/p0ny/p/7373464.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值