Pagini recente » Cod sursa (job #2072293) | Cod sursa (job #373864) | Cod sursa (job #1333255) | Cod sursa (job #862839) | Cod sursa (job #1456948)
#include <fstream>
#define NMAX 200001
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int heap[NMAX], pos[NMAX], v[NMAX], i, n=0, cronos = 0, type, nrquiz, value;
void change(int x, int y)
{
int aux;
aux = x;
x = y;
y = aux;
aux = pos[x];
pos[x] = pos[y];
pos[y] = aux;
}
int left_son(int x)
{
return 2*x;
}
int right_son(int x)
{
return 2*x + 1;
}
int father(int x)
{
return x/2;
}
void go_down(int k)
{
int aux;
do
{
aux = 0;
if (left_son(k) <= n)
{
aux = left_son(k);
if (right_son(k) <= n && heap[right_son(k)] < heap[aux])
aux = right_son(k);
if (heap[aux] > heap[k])
aux = 0;
}
if (aux)
{
change(heap[k], heap[aux]);
k = aux;
}
} while (aux);
}
void go_up(int k)
{
while (k > 1 && heap[k] < heap[father(k)])
{
change(heap[father(k)], heap[k]);
k = father(k);
}
}
void insert_heap(int x)
{
v[++cronos] = x;
heap[++n] = x;
pos[x] = n;
go_up(n);
}
void delete_heap(int k)
{
heap[k] = heap[n];
n--;
if (k > 1 && heap[k] < heap[father(k)])
go_up(k);
else
go_down(k);
}
int find_heap(int k)
{
for (int i=1; i<=n; ++i)
if (heap[i] == v[k]) return i;
}
int main()
{
f >> nrquiz;
while (nrquiz)
{
nrquiz --;
f >> type;
if (type == 1)
{
f >> value;
insert_heap(value);
}
if (type == 2)
{
f >> value;
delete_heap(find_heap(value));
}
if (type == 3)
{
g << heap[1] << '\n';
/* for (i=1; i<=n; ++i)
g<<heap[i]<<" ";
g<<'\n';*/
}
}
return 0;
}