Pagini recente » Cod sursa (job #1693642) | Cod sursa (job #2107713) | Cod sursa (job #43484) | Cod sursa (job #2736329) | Cod sursa (job #2739956)
#include <iostream>
#include <bits/stdc++.h>
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, b, a;
int v[1000000000], z = 0;
in >> n;
cout << "da";
for(int i = 1; i <= n; i++)
{
cout << "da";
in >> a;
if( a == 3)
cout << minim(heap) << "\n";
else if(a == 1)
{
in >> b;
v[z] = i;
z++;
insert_heap(heap, lungime, b);
}
else
{
in >> b;
delete_node(heap, lungime, v[b-1]);
for(int k = b - 1; k < z - 1; k++)
v[k] = v[k + 1];
z--;
}
}
return 0;
}