Cod sursa(job #1053360)

Utilizator PsychoAlexAlexandru Buicescu PsychoAlex Data 12 decembrie 2013 18:14:56
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 3.58 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <map>

std::ifstream fin("heapuri.in");
std::ofstream fout("heapuri.out");

int n;
std::vector<int> heapu;
std::map<int, int> hashu;
std::vector<int> moFoPos;

void insertu(int val)
{
    heapu.push_back(val);
    int i = heapu.size() / 2;
    int j = heapu.size() - 1;

    hashu[val] = j;

    while(i >= 1 && heapu[i] > val)
    {
        hashu[heapu[i]] = j;
        hashu[heapu[j]] = i;


        int aux = heapu[i];
        heapu[i] = heapu[j];
        heapu[j] = aux;



        j = i;
        i = i / 2;
    }
}

void delu(int position)
{
//    int val = heapu.back();

    int valInit = heapu.back();
position--;
//    std::cout<<"deleted: "<<"pos: "<<position<<";value: "<<moFoPos[position]<<"; pozitia in heapu: "<<hashu[moFoPos[position]]<<'\n';

//    std::cout<<"mofoposu: ";
//    for(int i = 0; i < moFoPos.size(); i++)
//    {
//        std::cout<<moFoPos[i]<<' ';
//    }
//    std::cout<<'\n';

    heapu[hashu[moFoPos[position]]] = heapu.back();
    hashu[heapu.back()] = hashu[moFoPos[position]];

    heapu.pop_back();

    int i = hashu[moFoPos[position]];
//    std::cout<<heapu.size()<<' '<<i<<": ";
    while(i * 2 < heapu.size())
    {
        int val = heapu[i*2];
        int p = 0;

        if(i*2 + 1 < heapu.size() && heapu[i*2+1] < heapu[i*2])
        {
            val = heapu[i*2+1];
            p = 1;
        }

        if(valInit > heapu[i*2+p])
        {
//            std::cout<<"; "<<hashu[heapu[i]]<<' '<<hashu[heapu[i*2+p]]<<"';'";
            int a1 = hashu[heapu[i]], a2 = hashu[heapu[i*2+p]];
            int aux = heapu[i];
            heapu[i] = heapu[i*2 + p];
            heapu[i*2 + p] = aux;

            aux = a1;
            a1 = a2;
            a2 = a1;

            hashu[heapu[i]] = a2;
            hashu[heapu[i*2 + p]] = a1;
        }
        else
        {
            break;
        }
        i = i * 2 + p;
    }
}

void citirea()
{
    int x, y;
    fin>>n;
    for(int i = 0; i < n; i++)
    {
        fin>>x;
        if(x == 1)
        {
            fin>>y;
            moFoPos.push_back(y);
            insertu(y);
//                    std::cout<<"insertu: ";
//                    for(int i = 1; i < heapu.size(); i++)
//                    {
//                        std::cout<<heapu[i]<<' ';
//                    }
//                    std::cout<<'\n';
//                    for(int i = 0; i < heapu.size(); i++)
//                    {
//                        std::cout<<heapu[i]<<' '<<hashu[heapu[i]]<<'\n';
//                    }
//                    std::cout<<'\n';
        }
        else
            if(x == 2)
            {
                fin>>y;
                delu(y);
//                    std::cout<<"delu: ";
//                    for(int i = 1; i < heapu.size(); i++)
//                    {
//                        std::cout<<heapu[i]<<' ';
//                    }
//                    std::cout<<'\n';
            }
            else
                if(x == 3)
                {
                    fout<<heapu[1]<<'\n';
//                    std::cout<<"show: ";
//                    for(int i = 1; i < heapu.size(); i++)
//                    {
//                        std::cout<<heapu[i]<<' ';
//                    }
//                    std::cout<<'\n';
//                    std::cout<<heapu[1]<<'\n';
                }
    }
}

void rezolvare()
{

}

int main()
{
    heapu.push_back(-1);
    citirea();
    rezolvare();
    return 0;
}