Cod sursa(job #1937968)

Utilizator sulzandreiandrei sulzandrei Data 24 martie 2017 14:53:37
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.48 kb
#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");
void solveA()
{
    set<int> myset;
    int t,pos[200003],x,op,i=0;
    f>>t;
    while(t--)
    {
        f>>op;
        switch(op)
        {
        case 1:
            i++;
            f>>x;
            myset.insert(x);
            pos[i] = x;
            break;
        case 2:
            f>>x;
            myset.erase(pos[x]);
            break;
        case 3:
                g<<*myset.begin()<<'\n';
            break;
        }
    }
}

constexpr int MAX_E = 200003;
// posi[i] the i-th inserted element's position in the heap, we use this so we know where is in the heap the el to delete
// heap[i] the element in the heap at pos i
// insi[i] the element on position i in the heap ,it's insertion time, we use this so we keep track of posi
int posi[MAX_E],insi[MAX_E];
int father(int i){return i/2;}
int lson(int i){return 2*i;}
int rson(int i){return 2*i+1;}
void swapthem(int *h,int k1,int k2)
{
    swap(posi[insi[k1]],posi[insi[k2]]);
    swap(insi[k1],insi[k2]);
    swap(h[k1],h[k2]);
}
void insert(int *h,int x,int &n,int time)
{
    h[++n] = x;
    insi[n] = time;
    posi[time] = n;
    int k = n;
    while(k>1 && h[k]<h[father(k)])
    {
        swapthem(h,k,father(k));
        k = father(k);
    }
}
void remove(int *h,int time,int &n)
{
    int pos = posi[time],posmin;
    swapthem(h,pos,n);
    n--;
    while(1)
    {
        posmin = pos;
        if( lson(pos)<=n && h[lson(pos)]<h[posmin])
            posmin = lson(pos);
        if( rson(pos)<=n && h[rson(pos)]<h[posmin])
            posmin = rson(pos);
        if(posmin == pos)                   //daca vre unul din fii are valoarea mai mica atunci ne ducem pe acel fiu
           break;
        else
        {
            swapthem(h,pos,posmin);
            pos = posmin;
        }
    }
}
void solveB()
{
    int heap[MAX_E],n=0,t,x,op,i=0;
    f>>t;
    int del = 0;
    while(t--)
    {
        f >> op;
        switch(op)
        {
        case 1:
            i++;
            f>>x;
            insert(heap,x,n,i);
            break;
        case 2:
            f>>x;
            del++;
            remove(heap,x,n);
            break;
        case 3:

            g<<heap[1]<<'\n';
            break;
        }
    }

}
int main()
{
    //solveA();
    solveB();
    return 0;
}