Pagini recente » Istoria paginii utilizator/brendon775 | Cod sursa (job #128664) | Cod sursa (job #205845) | Cod sursa (job #180531) | Cod sursa (job #271344)
Cod sursa(job #271344)
#include<fstream.h>
ifstream f("heapuri.in");
ofstream g("heapuri.out");
#define nmax 201
long x,y,n,m,p[nmax],nr=1;
struct heap
{
long inf,poz;
}h[nmax];
void coboara(long k)
{
long fiu=k;
if(2*k<=n)
{
if(h[fiu].inf>h[2*k].inf)
fiu=2*k;
if(2*k+1<=n&&h[fiu].inf>h[2*k+1].inf)
fiu=2*k+1;
if(fiu!=k)
{
p[h[fiu].poz]=k;
p[h[k].poz]=fiu;
heap z=h[fiu];
h[fiu]=h[k];
h[k]=z;
coboara(fiu);
}
}
}
void urca(long k)
{
long fiu=k;
if(k>1&&h[fiu].inf<h[k/2].inf)
{
p[h[fiu].poz]=k/2;
p[h[k/2].poz]=fiu;
heap z=h[fiu];
h[fiu]=h[k/2];
h[k/2]=z;
fiu=k/2;
urca(fiu);
}
}
void inserare(int y)
{
h[++n].inf=y;
h[n].poz=nr;
p[h[n].poz]=nr;
nr++;
urca(n);
}
void stergere(int y)
{
long k=p[y];
h[k]=h[n]; p[h[n].poz]=k;
n--;
if(k>1&&h[k].inf<h[k/2].inf)
urca(k);
else
coboara(k);
}
void citire()
{
long i;
f>>m;
n=0;
for(i=1;i<=m;i++)
{
f>>x;
switch(x)
{
case 1: f>>y;inserare(y);break;
case 2: f>>y;stergere(y);break;
case 3: g<<h[1].inf<<'\n';break;
}
}
}
int main()
{
citire();
f.close();
g.close();
return 0;
}