Pagini recente » Monitorul de evaluare | Cod sursa (job #2965628) | Cod sursa (job #1362468) | Cod sursa (job #754840) | Cod sursa (job #790563)
Cod sursa(job #790563)
//
// main.cpp
// Heapuri
//
// Created by Ioana Teoc on 9/16/12.
// Copyright (c) 2012 Ioana Teoc. All rights reserved.
//
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
#define MAXN 200005
int Heap[MAXN], Poz[MAXN], V[MAXN], n, cnt_H;
void citeste(){
f>>n;
}
void in_sus(int nod){
if(nod == 1) return;
if(V[Heap[nod]] < V[Heap[nod/2]]){
swap(Poz[Heap[nod]], Poz[Heap[nod/2]]);
swap(Heap[nod], Heap[nod/2]);
in_sus(nod/2);
}
}
void in_jos(int nod){
int fiu_min = nod;
int st = nod*2;
int dr = nod*2+1;
if(st <= cnt_H && V[Heap[st]] < V[Heap[fiu_min]])
fiu_min = st;
if(dr <= cnt_H && V[Heap[dr]] < V[Heap[fiu_min]])
fiu_min = dr;
if(fiu_min == nod) return;
swap(Poz[Heap[nod]], Poz[Heap[fiu_min]]);
swap(Heap[nod], Heap[fiu_min]);
in_jos(fiu_min);
}
void adauga(int x){
++cnt_H;
++V[0];
V[V[0]] = x;
Poz[V[0]] = cnt_H;
Heap[cnt_H] = V[0];
in_sus(cnt_H);
}
void sterge(int x){
int poz_h= Poz[x];
Poz[Heap[cnt_H]] = Poz[Heap[poz_h]];
Heap[poz_h] = Heap[cnt_H];
--cnt_H;
int nod = Poz[Heap[poz_h]];
if(nod > 1 && V[Heap[nod]] < V[Heap[nod/2]]){
in_sus(nod);
}
else
in_jos(nod);
}
void rezolva(){
for(int i = 1; i <= n; ++i){
int tip, x;
f >> tip;
if(tip == 1){
f >> x;
adauga(x);
}
else if (tip == 2){
f >> x;
sterge(x);
}
else {
g << V[Heap[1]] << "\n";
}
}
}
int main()
{
citeste();
rezolva();
f.close();
g.close();
return 0;
}