Cod sursa(job #1391805)

Utilizator pepsiM4A1Ozturk Arif pepsiM4A1 Data 18 martie 2015 10:34:51
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.13 kb
#include <stdio.h>
int cron[200001];
int n,m,lun;
int heap[200001],tip,nr;
void vertop(int p)
{
     if(heap[p]<heap[2*p]&&heap[p]<heap[2*p+1]) return;
     if(heap[p]>heap[2*p]&&heap[2*p]<heap[2*p+1])
     {
                                     int temp=heap[p];
                                     heap[p]=heap[2*p];
                                     heap[2*p]=temp;
     }
     else if(heap[p]>heap[2*p+1]&&heap[2*p+1]<heap[2*p])
     {
          int temp=heap[p];
          heap[p]=heap[2*p+1];
          heap[2*p+1]=temp;
     }
}
void ver(int p)
{
     int temp;
     if(p==1) vertop(p);
     else
     {
      if(heap[p]<heap[p/2])
      {
                          temp=heap[p];
                          heap[p]=heap[p/2];
                          heap[p/2]=temp;
                          ver(p/2);
      }
     }
}
void el(int p)
{
     int temp;
     temp=heap[m];
     heap[m]=heap[p];
     heap[p]=temp;
     heap[m]=1000000001;
     m--;
     if(p!=1) ver(p);
     else vertop(p);
}
int main()
{
    freopen ("heapuri.in","r",stdin);
    freopen ("heapuri.out","w",stdout);
    for(int i=1;i<=200000;i++) heap[i]=1000000001;
    scanf("%d",&n);
    for(int x=1;x<=n;x++)
    {
            scanf("%d",&tip);
            if(tip==1)
            {
                      scanf("%d",&nr);
                      m++;
                      lun++;
                      cron[lun]=nr;
                      heap[m]=nr;
                      ver(m);
                      //for(int i=1;i<=m;i++) printf("%d ",heap[i]);
                     // printf("\n");
            }
            else if(tip==2)
            {
                 scanf("%d",&nr);
                 for(int i=1;i<=m;i++)
                 {
                         if(heap[i]==cron[nr])
                         {
                                              el(i);
                                              break;
                         }
                 }
                 //for(int i=1;i<=m;i++) printf("%d ",heap[i]);
                // printf("\n");
            }
            else printf("%d\n",heap[1]);
    }
}