Cod sursa(job #1206902)

Utilizator ArkinyStoica Alex Arkiny Data 11 iulie 2014 14:33:56
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include<iostream>
#include<fstream>;
using namespace std;

typedef int Heap[1000];
Heap H;
int pos[1000];
inline int left_child(int K)
{
    return K*2;
}
inline int right_child(int K)
{
    return K*2 +1;
}
inline int father(int K)
{
    return K/2;
}
void sift(int N,int K)
{
  int son;
  int p=K;
  do
  {
     son=0;
     if(right_child(K) <= N)
     {
         son = (H[2*K]<H[2*K+1]) ? 2*K : (2*K+1);
     }
     else if(left_child(K)<=N)
        son=2*K;

     if(son && H[son]<H[K])
     {
        swap(H[son],H[K]);
        swap(pos[p],pos[son]);
        K=son;
     }
     else
        son=0;
  }while(son);
}
void peroclate(int N,int K)
{
    int p=K;
    while(father(K) && H[K]<H[K/2])
    {
        swap(H[K],H[K/2]);
        swap(pos[p],pos[K/2]);
        cout<<pos[1];
        K=K/2;
    }

}
void inserare(int &N,int key)
{
    H[++N]=key;
    pos[N]=N;
    peroclate(N,N);
}
void stergere(int &N,int K)
{
    H[K]=H[N];
    --N;
    if(father(K) && H[K/2]>H[K])
          peroclate(N,K);
    else
        sift(N,K);
}
int Min()
{
    return H[1];
}
int main()
{
    ifstream fi("heapuri.in");
    ofstream fo("heapuri.out");
    int i,k;
    int n=0;
    int t;
    fi>>t;
    for(int j=1;j<=t;j++)
    {
        fi>>i;
        if(i<3)
        {
            fi>>k;
            if(i==1)
                inserare(n,k);
            else
            {
                stergere(n,pos[k]);
            }
        }
        else
            fo<<Min()<<endl;
    }
    fi.close();
    fo.close();
    return 0;
}