Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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];
    }


}
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;
}