Pagini recente » Cod sursa (job #2693078) | Cod sursa (job #474962) | Clasament sunt_o_bestie_adevarata | Cod sursa (job #710953) | Cod sursa (job #523790)
Cod sursa(job #523790)
#include <iostream>
#include<fstream.h>
#define nmax 200010
using namespace std;
int n,L,nr;
int a[nmax], heap[nmax], poz[nmax];
void push(int x)
{
while(x>1 && a[heap[x]] < a[heap[x/2]])
{
int aux = heap[x];
heap[x] = heap[x/2];
heap[x/2] = aux;
poz[heap[x]]= x;
poz[heap[x/2]]=x/2;
x/=2;
}
}
void pop(int x)
{
int y=0;
if(2*y <= L && a[heap[x]] < a[heap[y/2]])
x=y/2;
if(2*y+1 <= L && a[heap[x]] < a[heap[y/2+1]])
x=y/2+1;
int aux = heap[x];
heap[x] = heap[y];
heap[y] = aux;
poz[heap[x]]= x;
poz[heap[y]]= y;
}
int main()
{
int op;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f>>n;
for(int i=0;i<n;i++)
{
f>>op;
if(op==1)
{
l++;
nr++;
a[nr]=x;
heap[l]=nr;
heap[nr]=l;
push(l);
}
else
if(op==2)
{
a[x]=-1;
push(poz[x]);
poz[h[1]] = 0;
h[1] = h[l--];
poz[h[1]] = 1;
if(l>1) pop(1);
}
else
if(op==3)
g<<a[heap[1]]<<"\n";
}
}
cout << "Hello world!" << endl;
return 0;
}