有现成的STL为什么不调?(虽然知道“实现”是要求自己写一个)
#include<bits/stdc++.h>
#include <queue>
using i64 = long long;
template<typename T, typename cmp = std::less<T>>
class Heap {
std::priority_queue<T, std::vector<T>, cmp> dq;
public:
Heap() {}
void push(T x) {
dq.push(x);
}
std::optional<T> top() {
if (dq.size()) return {dq.top()};
return {};
}
std::optional<T> pop() {
if (dq.size()) {
T res = top().value();
dq.pop();
return res;
}
return {};
}
};
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
Heap<int, std::less<int>> h;
int n;
std::cin >> n;
while (n--) {
std::string op;
std::cin >> op;
if (op == "push") {
int x;
std::cin >> x;
h.push(x);
} else if (op == "top") {
if (h.top().has_value()) {
std::cout << h.top().value() << "\n";
} else {
std::cout << "empty\n";
}
} else {
if (h.top().has_value()) {
std::cout << h.pop().value() << "\n";
} else {
std::cout << "empty\n";
}
}
}
return 0;
}

京公网安备 11010502036488号