Pagini recente » Cod sursa (job #2875140) | Cod sursa (job #1694389) | Cod sursa (job #1859919) | Cod sursa (job #2432741) | Cod sursa (job #2739948)
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int father(int k)
{
return k/2;
}
int left_son(int k)
{
return 2 * k;
}
int right_son(int k)
{
return 2 * k + 1;
}
int minim(int heap[])
{
return heap[1];
}
void shift_heap(int heap[], int n, int k)
{
while(2 * k <= n && (heap[k] > heap[left_son(k)] || heap[k] > heap[right_son(k)]))
{
if(heap[left_son(k)] > heap[right_son(k)])
{
swap(heap[right_son(k)], heap[k]);
k = right_son(k);
}
else
{
swap(heap[left_son(k)], heap[k]);
k = left_son(k);
}
}
}
void percolate_heap(int heap[], int n, int k)
{
while(k > 1 && heap[father(k)] > heap[k])
{
swap(heap[father(k)], heap[k]);
k = father(k);
}
}
void delete_node(int heap[], int &n, int k)
{
heap[k] = heap[n];
n--;
if(heap[k] > father(heap[k]))
shift_heap(heap, n, k);
else
{
percolate_heap(heap, n, k);
}
}
void insert_heap(int heap[], int &n, int k)
{
heap[++n] = k;
percolate_heap(heap, n, n);
}
ifstream in("heap.in");
ofstream out("heap.out");
int main()
{
int n, heap[1000000000], lungime = 0;
vector <int> v;
for(int i = 1; i <= n; i++)
{
int a;
in >> a;
if( a == 3)
out << minim(heap) << "\n";
else if(a == 1)
{
int b;
in >> b;
v.push_back(b);
insert_heap(heap, lungime, b);
}
else
{
int b;
in >> b;
int poz = 0;
while(poz < lungime && v[b] != heap[poz])
poz++;
v.erase(b);
delete_node(heap, lungime, poz);
}
}
return 0;
}