P3378 【模板】堆
题目描述
如题,初始小根堆为空,我们需要支持以下3种操作:
操作1: 1 x 表示将x插入到堆中
操作2: 2 输出该小根堆内的最小数
操作3: 3 删除该小根堆内的最小数
binary_heap
/*
584ms/468ms
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1000010;
int n;
int key[N];
void Up(int k) {
for (; k > 1 && key[k] < key[k / 2]; k /= 2)
swap(key[k], key[k / 2]);
return;
}
void Down(int k) {
for (; k * 2 <= n; ) {
int j = k * 2;
if (j + 1 <= n && key[j] > key[j + 1]) ++j;
if (key[k] > key[j]) {
swap(key[k], key[j]);
k = j;
} else break;
}
return;
}
void Pop() {
if (!n) throw;
key[1] = key[n--];
Down(1);
return;
}
void Push(int x) {
key[++n] = x;
Up(n);
return;
}
inline int Top() {
return key[1];
}
int main() {
int m;
scanf("%d", &m);
while (m--) {
int t;
scanf("%d", &t);
if (t == 1) {
int x;
scanf("%d", &x);
Push(x);
} else if (t == 2) printf("%d
", Top());
else Pop();
}
return 0;
}
priority_queue
/*
1320ms/480ms
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> > pq;
int main() {
int m;
scanf("%d", &m);
while (m--) {
int t;
scanf("%d", &t);
if (t == 1) {
int x;
scanf("%d", &x);
pq.push(x);
} else if (t == 2) printf("%d
", pq.top());
else pq.pop();
}
return 0;
}
heap
/*
676ms/480ms
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000010;
int n;
int key[N];
void Push(int x) {
key[++n] = x;
push_heap(key + 1, key + n + 1, greater<int>());
return;
}
void Pop() {
pop_heap(key + 1, key + n + 1, greater<int>());
--n;
return;
}
inline int Top() {
return key[1];
}
int main() {
int m;
scanf("%d", &m);
n = 0;
// make_heap(key + 1, key + n + 1, greater<int>());
while (m--) {
int t;
scanf("%d", &t);
if (t == 1) {
int x;
scanf("%d", &x);
Push(x);
} else if (t == 2) printf("%d
", Top());
else Pop();
}
return 0;
}
pb_ds库
头文件
#include <ext/pb_ds/priority_queue.hpp>
命名空间
using namespace __gnu_pbds;
定义
template < typename Value_Type ,
typename Cmp_Fn = std :: less < Value_Type > ,
typename Tag = pairing_heap_tag ,
typename Allocator = std :: allocator < char > >
class priority_queue
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
__gnu_pbds::priority_queue<int, greater<int> > pq;
// __gnu_pbds::priority_queue<int, greater<int>, __gnu_pbds::pairing_heap_tag> pq;
// __gnu_pbds::priority_queue<int, greater<int>, __gnu_pbds::binomial_heap_tag> pq;
// __gnu_pbds::priority_queue<int, greater<int>, __gnu_pbds::rc_binomial_heap_tag> pq;
// __gnu_pbds::priority_queue<int, greater<int>, __gnu_pbds::binary_heap_tag> pq;
int main() {
int m;
scanf("%d", &m);
while (m--) {
int t;
scanf("%d", &t);
if (t == 1) {
int x;
scanf("%d", &x);
pq.push(x);
} else if (t == 2) printf("%d
", pq.top());
else pq.pop();
}
return 0;
}
与std::priority queue 的用法基本相同,有size(),empty(),push(const T),top(),pop(),clear()。
配对堆(pairing_heap)、二叉堆(binary_heap)、二项堆(binomial_heap)、冗余计数二项堆(redundant-counter binomial_heap,没找到通用译名,故自行翻译)、经改良的斐波那契堆(thin_heap)。
懒,留坑