堆
1.插入
void put(int x) {
heap[++len] = x;
int fa, now = len;
while(now > 1) {
fa = now >> 1;
if(heap[fa] <= heap[now]) break;
swap(heap[fa], heap[now]);
now = fa;
}
return;
}
2.返回堆顶值
int get() {
return heap[1];
}
3.取出堆顶值
void pop() {
heap[1] = heap[len--];
int son, now = 1;
while((now << 1) <= len) {
son = now << 1;
if(heap[son + 1] <= heap[son]) son = son + 1;
if(heap[now] <= heap[son]) break;
swap(heap[now], heap[son]);
now = son;
}
return;
}
转载请注明原文地址:https://blackberry.8miu.com/read-33974.html