Cod sursa(job #2190195)

Utilizator ibogdan25Ilie Ionut ibogdan25 Data 30 martie 2018 00:25:49
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.73 kb
// Heap.cpp : Defines the entry point for the console application.
//
#include <algorithm>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <stdio.h>

#include <vector>

using namespace std;

const int MAX_HEAP_SIZE = 200002;
int H[MAX_HEAP_SIZE], nH, History[MAX_HEAP_SIZE];
//template <class T> void swap(T& a, T& b)
//{
//	T c(a); a = b; b = c;
//}


inline int father(int x) {
	return x / 2;
}
inline int left_son(int x) {
	return x * 2;
}
inline int right_son(int x) {
	return x * 2 + 1;
}

void sift(int k) {
	int son = 0;
	do {
		son = 0;
		if (left_son(k) <= nH) {
			son = left_son(k);
			if (right_son(k) <= nH  && H[right_son(k)] > H[son]) {
				son = right_son(son);
			}
			if (H[son] < H[k]) {
				son = 0;
			}
		}
		if (son) {
			swap(H[k], H[son]);
			k = son;
		}
	} while (son);
}

void percolate(int k) {
	while (k > 1 && H[k] > H[father(k)]) {
		swap(H[k], H[father(k)]);
		k = father(k);
	}
}
void cut(int key) {
	H[key] = H[nH];
	nH--;

	if (key > 1 && H[key] > H[father(key)]) {
		percolate(key);
	}
	else {
		sift(key);
	}
}

void insert(int value) {
	H[++nH] = value;
	percolate(nH);
}
int getMax() {
	return H[1];
}
int main()
{
	ifstream f("heapuri.in");
	ofstream g("heapuri.out");
	int m = 0;
	f >> m;
	while (m--) {
		int optiune = 0;
		f >> optiune;
		if (optiune == 3) {
			g << -getMax() << "\n";
		}
		else {
			int val = 0;
			f >> val;
			if (optiune == 1) {
				History[nH + 1] = -val;
				insert(-val);
			}
			else {
				int valCautata = History[val];
				for (int i = 1; i <= nH; i++) {
					if (H[i] == valCautata) {
						cut(i);
						break;
					}
				}
			}
		}
	}
    return 0;
}