Pagini recente » Cod sursa (job #438383) | Cod sursa (job #1068118) | Cod sursa (job #439036) | Cod sursa (job #2222111) | Cod sursa (job #2190212)
// 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");
FILE * pFile;
pFile = fopen("heapuri.in", "r");
int m = 0;
int counter = 0;
fscanf(pFile, "%d", &m);
while (m--) {
int optiune = 0;
fscanf(pFile, "%d", &optiune);
if (optiune == 3) {
g << -getMax() << "\n";
}
else {
int val = 0;
fscanf(pFile, "%d", &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;
}