Cod sursa(job #2190210)

Utilizator ibogdan25Ilie Ionut ibogdan25 Data 30 martie 2018 02:20:25
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 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;
}
//sift = jos
void jos(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(k);
			}
			if (H[son] <= H[k]) {
				son = 0;
			}
		}
		if (son) {
			swap(H[k], H[son]);
			k = son;
		}
	} while (son);
}

void sus(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)]) {
		sus(key);
	}
	else {
		jos(key);
	}
}

void insert(int value) {
	H[++nH] = value;
	sus(nH);
}
int getMax() {
	return H[1];
}
int main()
{
	ifstream f("heapuri.in");
	ofstream g("heapuri.out");
	int m = 0;
	int counter = 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) {
                counter++;
				History[counter] = -val;
				insert(-val);

			}
			else {
				int valCautata = History[val];
				for (int i = 1; i <= nH; i++) {
					if (H[i] == valCautata) {
						cut(i);
						break;
					}
				}
			}
		}
	}
    return 0;
}