大顶堆
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct heap{
int size, a[N<<1];
heap(){
size = 0;
memset(a, 0, sizeof(a));
}
void Pushup(int k){
while(k > 1){
if(a[k] > a[k>>1]){
swap(a[k], a[k>>1]);
}
k >>= 1;
}
}
void Pushdown(int k){
while(k<<1 <= size){
int kk = k<<1;
if(kk < size && a[kk] < a[kk+1]){
kk++;
}
if(a[k] < a[kk]){
swap(a[k], a[kk]);
k = kk;
}
else return;
}
}
void push(int k){
a[++size] = k;
Pushup(size);
}
void pop(){
swap(a[1], a[size]);
size--;
Pushdown(1);
}
int top(){
return a[1];
}
bool empty(){
return size;
}
};
int main(){
heap k;
int a[6] = {1, 4, 2, 6, 3, 9};
for(int i = 0; i < 6; i++){
k.push(a[i]);
}
printf("top = %d\n", k.top());
k.pop();
printf("top = %d\n", k.top());
k.push(8);
printf("top = %d\n", k.top());
}
#include <bits/stdc++.h>
using namespace std;
vector<int> k;
bool cmp(int a, int b){
return a < b;
}
int main(){
int a[6] = {1, 4, 2, 9, 6, 3};
for(int i = 0; i < 6; i++){
k.push_back(a[i]);
}
make_heap(k.begin(), k.end(), cmp);
cout << "top = " << k[0] << endl;
pop_heap(k.begin(), k.end(), cmp);
cout << "top = " << k[0] << endl;
k.push_back(8);
push_heap(k.begin(), k.end(), cmp);
cout << "top = " << k[0] << endl;
}