Cod sursa(job #1785865)

Utilizator OleaginoasaCristina Oleaginoasa Data 22 octombrie 2016 01:48:46
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.75 kb
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

vector<long long int> heapVector;

void addElement(long long int x){
    heapVector.push_back(x);
    int pos = heapVector.size()-1;
    
    while(pos/2 >= 0 && heapVector[pos/2] > x){
        swap(heapVector[pos/2],heapVector[pos]);
        pos = pos/2;
    }
}

void deleteElement(long long int x){
    int i = 0, size = heapVector.size(), pos = 0;
    long long int min = 32000;
    while(i < size && heapVector[i] != x){
        i++;
    }
    heapVector[i] = heapVector[size-1];
    heapVector.erase(heapVector.begin() + size-1);
    
    while( (2*i+1) < size && (2*i+2) < size && (heapVector[2*i+1] < heapVector[i] || heapVector[2*i+2] < heapVector[i])){
        if(min > heapVector[2*i+1]){
            min = heapVector[2*i+1];
            pos = 2*i+1;
        }
        if(min > heapVector[2*i+2]){
            min = heapVector[2*i+2];
            pos = 2*i+2;
        }
        swap(heapVector[i], heapVector[pos]);
        i = pos;
    }
}


long long int minElement(){
    return heapVector[0];
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int Q, type;
    vector<long long int> v;
    long long int number;
    freopen("heapuri.in", "r", stdin);
    freopen("heapuri.out","w", stdout);
    
    scanf("%d", &Q);
    for(int i = 0; i < Q; ++i){
        scanf("%d", &type);
        if(type == 1){
            scanf("%lld", &number);
            addElement(number);
            v.push_back(number);
            
        }
        else if(type == 2){
            scanf("%lld", &number);
            deleteElement(v[number-1]);
        }
        else
            printf("%lld \n", minElement());
    }
    return 0;
}