Pagini recente » Cod sursa (job #1622132) | Cod sursa (job #2878329) | Cod sursa (job #1825606) | Cod sursa (job #2955660) | Cod sursa (job #1082961)
#include <iostream>
#include <cstdio>
#include <algorithm>
#define nmax 200001
FILE *f,*g;
using namespace std;
int pozitie,POZ[nmax],POZ2[nmax],k,k2;
int n;
void urca(int *heap, int &poz)
{
while(poz >= 1 && heap[poz] < heap[poz/2])
{
swap(heap[poz] , heap[poz/2]);
pozitie = poz;
poz /= 2;
}
}
void coboara(int *heap, int &poz)
{
while(poz * 2 <= n && heap[poz] > heap[poz*2])
{
swap(heap[poz] , heap[poz*2]);
poz *= 2;
}
}
void insert_heap(int *heap, int val)
{
heap[++n] = val;
pozitie = n;
urca(heap,n);
}
void elimina(int *heap, int poz)
{
heap[poz] = heap[n--];
coboara(heap,poz);
urca(heap,poz);
}
int main()
{
f=fopen("heapuri.in","r");
g=fopen("heapuri.out","w");
int i,j,N,heap[nmax],x,c;
n = 0;
fscanf(f,"%d",&N);
for(i=1 ; i<=N ; i++)
{
fscanf(f,"%d%d",&c,&x);
if(c == 1)
{
insert_heap(heap,x);
POZ[++k] = pozitie;
continue;
}
if(c == 2)
{
elimina(heap,POZ[x]);
continue;
}
fprintf(g,"%d\n",heap[1]);
}
fclose(f);
fclose(g);
return 0;
}