本文整理汇总了C++中MaxHeap::extractMax方法的典型用法代码示例。如果您正苦于以下问题:C++ MaxHeap::extractMax方法的具体用法?C++ MaxHeap::extractMax怎么用?C++ MaxHeap::extractMax使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MaxHeap
的用法示例。
在下文中一共展示了MaxHeap::extractMax方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
int n;
cin >> n;
MaxHeap mHeap;
uint tmp;
for (int i = 0; i < n; i++){
cin >> tmp;
if (i <= n / 2){
mHeap.insert(tmp);
}
else{
mHeap.insert(tmp);
mHeap.extractMax();
}
}
uint num1, num2;
if (n % 2 != 0){
cout << mHeap.extractMax() << ".0" << endl;
}
else{
num1 = mHeap.extractMax();
num2 = mHeap.extractMax();
if ((num1 + num2) % 2 == 0){
cout << (num1 + num2) / 2 << ".0" << endl;
}
else{
cout << (num1 + num2) / 2 << ".5" << endl;
}
}
return 0;
}
示例2: handleQuery
float handleQuery(float prev,int next,MaxHeap& maxheap,MinHeap& minheap){
if(next>prev){
if(minheap.n>maxheap.n){
maxheap.insert(minheap.extractMin());
}
minheap.insert(next);
}else{
if(maxheap.n>minheap.n){
minheap.insert(maxheap.extractMax());
}
maxheap.insert(next);
}
if((maxheap.n+minheap.n)%2==0){
float l = maxheap.a[0];
float r = minheap.a[0];
return (l+r)/2;
}else{
if(maxheap.n>minheap.n)
return maxheap.a[0];
return minheap.a[0];
}
}
示例3: testHeap
double testHeap(T testData[], int n, bool isHeapify) {
clock_t startTime = clock();
MaxHeap<T> *maxHeap;
if (isHeapify) {
maxHeap = new MaxHeap<T>(testData, n);
} else {
maxHeap = new MaxHeap<T>();
for (int i = 0; i < n; ++i) {
maxHeap->add(testData[i]);
}
}
T *arr = new T[n];
for (int j = 0; j < n; ++j) {
arr[j] = maxHeap->extractMax();
}
for (int k = 1; k < n; ++k) {
assert(arr[k - 1] >= arr[k]);
}
std::cout << "Test MaxHeap completed." << std::endl;
clock_t endTime = clock();
return double(endTime - startTime) / CLOCKS_PER_SEC;
}