Pagini recente » Cod sursa (job #2063668) | Cod sursa (job #885068) | Cod sursa (job #147075) | Cod sursa (job #919097) | Cod sursa (job #473079)
Cod sursa(job #473079)
// Heapuri.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include "stdio.h"
FILE *f=fopen("heapuri.in", "r");
FILE *g=fopen("heapuri.out", "w");
int n;
int v[200001];
int poz[200001];
int vpoz[200001];
void inserare(int x, int nr, int loc)
{
v[loc]=x;
poz[loc]=nr;
vpoz[nr]=loc;
int tata=loc/2;
int fiu=loc;
while (x<v[tata])
{
vpoz[nr]=tata;
vpoz[poz[tata]]=fiu;
int h=v[tata]; v[tata]=v[fiu]; v[fiu]=h;
h=poz[tata]; poz[tata]=poz[fiu]; poz[fiu]=h;
fiu=tata;
tata=tata/2;
}
}
void stergere(int nr, int loc)
{
int tata=vpoz[nr];
v[vpoz[nr]]=v[loc];
v[loc]=0;
poz[vpoz[nr]]=poz[loc];
vpoz[poz[loc]]=vpoz[nr];
poz[loc]=0;
vpoz[nr]=0;
int fiu=tata*2;
if (v[fiu]>v[fiu+1] && fiu<loc-1)
fiu++;
while (v[tata]>v[fiu] && fiu<loc)
{
vpoz[poz[tata]]=fiu;
vpoz[poz[fiu]]=tata;
int h=v[tata]; v[tata]=v[fiu]; v[fiu]=h;
h=poz[tata]; poz[tata]=poz[fiu]; poz[fiu]=h;
tata=fiu;
fiu=tata*2;
if (v[fiu]>v[fiu+1])
fiu++;
}
}
int main()
{
fscanf(f, "%d", &n);
int nr=0, cnr=0;
for (int i=1; i<=n; ++i)
{
int o, x;
fscanf(f, "%d", &o);
if (o==1)
{
fscanf(f, "%d", &x);
nr++;
cnr++;
inserare(x, nr, cnr);
}
else
if (o==2)
{
fscanf(f, "%d", &x);
stergere(x, cnr);
cnr--;
}
else
fprintf(g, "%d\n", v[1]);
}
return 0;
}