Cod sursa(job #1032995)

Utilizator vladvaldezVlad Dimulescu vladvaldez Data 16 noiembrie 2013 11:59:48
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <stdio.h>

using namespace std;
FILE *f=fopen("heapuri.in","r");
FILE *g=fopen("heapuri.out","w");

int n,m,i,x,h[200050],poz[200050],v[200050],nr,N,tip;

void swapp(int a,int b)
{int aux;
aux=h[a];
h[a]=h[b];
h[b]=aux;
poz[h[a]]=a;
poz[h[b]]=b;
}


void  cern (int k)
{
int st=2*k,aux=0;
if (st<=nr){

 if(2*k+1<=nr && v[h[st+1]]<v[h[st]])st++;
 if (v[h[st]]<v[h[k]]){swapp(k,st);cern(st);}
 }
}

void urc(int k)
{int t=k/2;

  if (t>0)if(v[h[k]]<v[h[t]]) {
  swapp(k,t);
  urc(t);
  }

}


void cut(int k)
 {int aux;


aux=h[k];
h[k]=h[nr];
h[nr]=aux;
poz[h[k]]=k;
poz[h[nr]]=nr;
nr--;

cern(k);
}


void insert()
{
 h[++nr]=n;
 poz[n]=nr;
 urc(nr);
}



int main()
{

fscanf(f,"%d",&N);
for(i=1;i<=N;++i)
{
 fscanf(f,"%d",&m);
 if (m==1){fscanf(f,"%d",&x);v[++n]=x;insert();}
 else if (m==2){fscanf(f,"%d",&x);cut(poz[x]);}
 else if (m==3)fprintf(g,"%d\n",v[h[1]]);
}

fclose(g);
return 0;
}