Pagini recente » Istoria paginii runda/simucassi | Istoria paginii utilizator/dinu_blanovschi | Istoria paginii runda/test_sdasjdkasdasjk | loool | Cod sursa (job #2743503)
#include <bits/stdc++.h>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int heap[200001],order[200001];
int ctHeap = 0,ctOrder = 0;
void push(int k)
{
ctHeap++;
heap[ctHeap] = k;
int i=ctHeap;
// Fix the min heap property if it is violated
while (i != 0 && heap[(i-1)/2] > heap[i])
{
swap(heap[i], heap[(i-1)/2]);
i = (i-1)/2;
}
}
void heapify(int i)
{
int l = 2*i+1;
int r = 2*i;
int smallest = i;
if (l < ctHeap && heap[l] < heap[i])
smallest = l;
if (r < ctHeap && heap[r] < heap[smallest])
smallest = r;
if (smallest != i)
{
swap(heap[i], heap[smallest]);
heapify(smallest);
}
}
void pop(int value)
{
int poz=0;
for(int i=1;i<=ctHeap;i++)
if(heap[i]==value)
{
poz = i;
break;
}
heap[poz] = INT_MAX;
heapify(1);
ctHeap--;
heap[ctHeap+1] = 0;
}
int main() {
int n;
f>>n;
for(int i=0;i<n;i++)
{
int op,value;
f>>op;
if(op==1)
{
f>>value;
push(value);
ctOrder++;
order[ctOrder] = value;
}
else if(op==2)
{
f>>value;
pop(order[value]);
}
else if(op==3)
{
g<<heap[1]<<'\n';
}
}
return 0;
}