Pagini recente » Cod sursa (job #1577433) | Cod sursa (job #2473386) | Cod sursa (job #1373153) | Cod sursa (job #1552744) | Cod sursa (job #1340562)
#include <iostream>
#include <fstream>
#define N 200004
using namespace std;
int h[N],top;
int v[N],nr;
int Tata(int nod)
{
return nod/2;
}
int Lson(int nod)
{
return nod*2;
}
int Rson(int nod)
{
return nod*2+1;
}
void Percolate(int k)
{
int key=h[k];
while(k>1 && h[Tata(k)]>key)
{
h[k]=h[Tata(k)];
k=Tata(k);
}
h[k]=key;
}
void Sift(int k)
{
int son=Lson(k);
while(son)
{
son=0;
if(Lson(k)<=top)
{
son=Lson(k);
if(Rson(k)<=top && h[Rson(k)]<h[son])
son=Rson(k);
if(h[son]>=h[k]) son=0;
}
if (son)
{
swap(h[son],h[k]);
k=son;
}
}
}
void Op1(int x)
{
top++;
h[top]=x;
Percolate(top);
}
void Op2(int x)
{
h[x]=h[top];
top--;
if(x>1 && h[x]<h[Tata(x)])
Percolate(x);
else Sift(x);
}
ofstream fout("heapuri.out");
void Op3()
{
fout<<h[1]<<"\n";
}
void Afisare()
{
for(int i=1; i<=top; i++)
cout<<h[i]<<" ";
cout<<"\n";
}
int CautaB(int x)
{
for(int i=1; i<=top; i++)
if(h[i]==x) return i;
}
int Cauta1(int x,int poz)
{
//cout<<h[poz<<" "<<x<<"\n";
if(h[poz]==x) return poz;
if(Rson(poz)<=top && h[Rson(poz)]<=x) return Cauta1(x,Rson(poz));
if(Lson(poz)<=top && h[Lson(poz)]<=x) return Cauta1(x,Lson(poz));
}
int main()
{
int k,com,x,poz,i;
ifstream fin("heapuri.in");
fin>>k;
for(i=1; i<=k; i++)
{
fin>>com;
if(com==1)
{
fin>>x;
Op1(x);
v[++nr]=x;
}
if(com==2)
{
fin>>x;
poz=CautaB(v[x]);
//cout<<poz<<"- ";
//Afisare();
Op2(poz);
}
if(com==3) Op3();
}
return 0;
}