Pagini recente » Cod sursa (job #2906913) | Cod sursa (job #2156248) | Cod sursa (job #569139) | Cod sursa (job #354992) | Cod sursa (job #2028655)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int poz [200010],nrel,nrt;
struct st
{
int val, ord;
}heap[256010];
void schimba (int p1, int p2)
{
st aux;
poz[heap[p1].ord]=p2;
poz[heap[p2].ord]=p1;
aux=heap[p1];
heap[p1]=heap[p2];
heap[p2]=aux;
}
void sus (int pos)
{
if (pos/2!=0 && heap[pos/2].val>heap[pos].val) {schimba (pos/2, pos);sus(pos/2);}
}
void jos (int p)
{
int fs=p*2, fd=p*2+1;
int best1, best2;
if (heap[fs].val>heap[fd].val) {best1=fd; best2=fs;}
else {best1=fs;best2=fd;}
if (heap[best1].val<heap[p].val && best1<=nrel) {schimba (best1, p);jos(best1); }
else if (heap[best2].val<heap[p].val && best2<=nrel) {schimba(best2,p);jos(best2);}
}
void adauga (int x)
{
nrel++;
nrt++;
heap[nrel].val=x;
heap[nrel].ord=nrt;
poz[nrt]=nrel;
sus(nrel);
}
void sterge (int p)
{
//cout<<poz[p]<<" "<<nrel<<"\n\n";
int aux=poz[p];
//g<<aux<<"\n";
schimba (poz[p], nrel);
nrel--;
sus (aux);
jos (aux);
}
int main()
{
int op,c;
f>>op;
int i, j;
int x, pos;
for(i=1;i<=op;i++)
{
f>>c;
if (c==1) {f>>x;adauga(x);}
if (c==2) {f>>pos;sterge(pos);}
if (c==3) g<<heap[1].val<<"\n";
/*
for (j=1;j<=nrel;j++)
cout<<heap[j].val<<" ";
cout<<"\n";
for (j=1;j<=nrel;j++)
cout<<heap[j].ord<<" ";
cout<<"\n";
for (j=1;j<=nrt;j++) cout<<poz[j]<<" ";
cout<<"\n\n";
*/
}
return 0;
}