试读部分
第二道独立ac的绿题。 using i64 = long long;
template<typename T>
class smallest_heap {
private:
T heap[100005];
int len;
public:
smallest_heap();
void push(T const&);
void pop();
T top();
int size();
bool empty();
};
template<typename T>
smallest_heap<T>::smallest_heap() {
len = 0;
memset(heap, 0, sizeof(heap));
}
template<typename T>
void smallest_heap<T>::push(T const& n) {
heap[++len] = n;
int son = len, father = son / 2;
while (heap[son] > heap[father]&&father>=1) {
std::swap(heap[son], heap[father]);
son = father, father = son / 2;
}
}
template<typename T>
void smallest_heap<T>::pop() {
std::swap(heap[1], heap[len]);
heap[len--] = 0;
int son = 2, father = 1;
while (son <= len) {
if (son<len && heap[son]<heap[son + 1])son++;
if (heap[son] > heap[father]) {
std::swap……
本文为 VIP 文章,已展示部分内容,
开通 VIP 后查看全文
觉得这篇文章有帮助?
点个赞,或赞赏支持作者持续创作