Pagini recente » Cod sursa (job #2004213) | Cod sursa (job #1907597) | Cod sursa (job #358044) | Cod sursa (job #2746307) | Cod sursa (job #1442563)
#include <fstream>
//#include "Heap.h"
#define DMAX 200004
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
enum HeapType { MAXHEAP=1, MINHEAP=2 };
class Heap
{
private:
int n;
int *H;
HeapType tip;//1=MaxHeap, 2=MinHeap
friend void Combinare(const int& i, Heap& a);
public:
Heap(const HeapType& t=MAXHEAP);
Heap(const int *v, const int& lg, const HeapType& t=MAXHEAP);
~Heap();
Heap(const Heap& other);
Heap& operator = (const Heap& other);
int GetType() { return tip; }
bool Empty();
void Delete();
int Size();
int Top();
void Push(const int& x);
int Pop();
friend void Swap(Heap& a, Heap& b);
//friend void HeapSort(const Heap& a, int *v);
friend ostream& operator << (ostream& out, const Heap& x);
};
Heap::Heap(const HeapType& t)
{
H=0; n=0;
tip=t;
}
Heap::Heap(const int *v, const int& lg, const HeapType& t)
{
n=lg;
H=new int [n+1];
tip=t;
int i;
for(i=1;i<=n;++i)
H[i]=v[i];
for(i=n/2;i>0;--i)
Combinare(i, *this);
}
Heap::~Heap()
{
if(H) H=0;
n=0; tip=MAXHEAP;
}
Heap::Heap(const Heap& other)
{
if(H) delete H;
n=other.n;
H=new int [n+1];
int i;
for(i=1;i<=n;++i)
H[i]=other.H[i];
tip=other.tip;
}
Heap& Heap::operator = (const Heap& other)
{
if (this==&other) return *this;
if(H) delete H;
n=other.n;
H=new int [n+1];
int i;
for(i=1;i<=n;++i)
H[i]=other.H[i];
tip=other.tip;
return *this;
}
bool Heap::Empty()
{
if(n==0) return 1;
return 0;
}
void Heap::Delete()
{
if(H) H=0;
n=0; tip=MAXHEAP;
}
int Heap::Size()
{
return n;
}
int Heap::Top()
{
if(n>0) return H[1];
return 0;
}
void Heap::Push(const int& x)
{
Heap temp;
temp.n=n+1;
temp.H=new int [temp.n+1];
temp.tip=tip;
int i;
for(i=1;i<=n;++i)
temp.H[i]=H[i];
int fiu=temp.n;
int tata=temp.n/2;
if(tip==MAXHEAP)
{
while(tata && temp.H[tata]<x)
{
temp.H[fiu]=temp.H[tata];
fiu=tata; tata=fiu/2;
}
}
else//MINHEAP
{
while(tata && temp.H[tata]>x)
{
temp.H[fiu]=temp.H[tata];
fiu=tata; tata=fiu/2;
}
}
temp.H[fiu]=x;
*this=temp;
}
int Heap::Pop()
{
int minim=H[1];
Heap temp;
temp.n=n-1;
temp.H=new int [temp.n+1];
temp.tip=tip;
int i;
for(i=1;i<n;++i)
temp.H[i]=H[i];
temp.H[1]=H[n];
Combinare(1, temp);
*this=temp;
return minim;
}
void Swap(Heap& a, Heap& b)
{
Heap temp;
temp=a;
a=b;
b=temp;
}
void Combinare(const int& i, Heap& a)
{
int x=a.H[i], tata=i, fiu=2*i;
if(a.tip==MAXHEAP)
{
while(fiu<=a.n)
{
if(fiu+1<=a.n && a.H[fiu]<a.H[fiu+1]) ++fiu;
if(a.H[fiu]>x)
{
a.H[tata]=a.H[fiu];
tata=fiu; fiu=2*tata;
}
else break;
}
}
else//MINHEAP
{
while(fiu<=a.n)
{
if(fiu+1<=a.n && a.H[fiu+1]<a.H[fiu]) ++fiu;
if(a.H[fiu]<x)
{
a.H[tata]=a.H[fiu];
tata=fiu; fiu=2*tata;
}
else break;
}
}
a.H[tata]=x;
}
ostream& operator << (ostream& out, const Heap& x)
{
int i;
for(i=1;i<=x.n;++i)
out<<x.H[i]<<' ';
return out;
}
int n;
Heap H(MINHEAP);
int v[DMAX], nr;
void citire();
void stergere(int x);
int main()
{
citire();
return 0;
}
void citire()
{
int i, x, val;
fin>>n;
for(i=1;i<=n;++i)
{
fin>>x;
if(x==1)
{
fin>>val;
H.Push(val);
v[++nr]=val;
//fout<<"introdus: "<<val<<" minim: "<<H.Top()<<'\n';
}
if(x==2)
{
fin>>val;
stergere(val);
}
if(x==3)
fout<<H.Top()<<'\n';
}
}
void stergere(int x)
{
int i, val, top;
Heap extras;
val=v[x];
while(H.Top()!=val)
{
extras.Push(H.Pop());
}
H.Pop();
while(!extras.Empty())
H.Push(extras.Pop());
}