Pagini recente » Cod sursa (job #3280573) | Cod sursa (job #2748527) | Cod sursa (job #2423426) | Cod sursa (job #2424010) | Cod sursa (job #2691274)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 200100;
struct HeapNormal
{
int n;
int v[NMAX];
HeapNormal(){
n = 0;
}
void GoUp(int x) {
// Incearca sa il impinga pe x in x/2, x/4 ....
while (x != 1 && v[x / 2] > v[x]) {
swap(v[x], v[x/2]);
x /= 2;
}
}
void GoDown(int x) {
// incearca sa il mute pe x in (2x / 2x + 1) ..
if (2 * x > n)
return;
if (2 * x == n){
if (v[x] > v[2 * x]){
swap(v[x], v[2*x]);
GoDown(2*x);
}
return;
}
if (v[2*x] <= v[2*x + 1] && v[2*x] < v[x]){
swap(v[x], v[2 * x]);
GoDown(2 * x);
}
else if (v[2 * x + 1] <= v[2 * x] && v[2 * x + 1] < v[x]){
swap(v[2 * x + 1], v[x]);
GoDown(2 * x + 1);
}
}
int GetMinim() {
// Gaseste elementul minim
if(n == 0)
return 2e9;
return v[1];
}
void Adauga(int x) {
// Il adauga pe x
n++;
v[n] = x;
GoUp(n);
}
void StergeMinim() {
// Sterge elementul minim
swap(v[n], v[1]);
n--;
GoDown(1);
}
};
struct Heap
{
HeapNormal normal, stergere;
int GetMinim() {
// Minimul
while(normal.GetMinim() == stergere.GetMinim()){
normal.StergeMinim();
stergere.StergeMinim();
}
return normal.GetMinim();
}
void Adauga(int x) {
// Adauga x
normal.Adauga(x);
}
void Remove(int x) {
// Sterge pe x
stergere.Adauga(x);
}
};
int elemente[NMAX], cnt_elemente;
int main()
{
ifstream in("heapuri.in");
ofstream out("heapuri.out");
Heap heap;
int N;
in >> N;
for (int i = 0; i < N; i++) {
int op;
in >> op;
if (op == 3) {
// Afiseaza minimul
out << heap.GetMinim() << '\n';
}
else if (op == 1) {
int x;
in >> x;
// Adauga x, si salveaza-l in elemente
cnt_elemente++;
elemente[cnt_elemente] = x;
heap.Adauga(x);
}
else {
int x;
in >> x;
// Vrei sa stergi al x-lea element adaugat
heap.Remove(elemente[x]);
}
}
return 0;
}