Cod sursa(job #3238114)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 19 iulie 2024 19:31:02
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>
using namespace std;

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

int heap[200001];
int a[100001];
int heapLen;
int aLen;

void updateHeap(int pos) {
    int j = pos;
    while (j >= 2 && heap[j] < heap[j / 2]) {
        swap(heap[j], heap[j / 2]);
        j /= 2;
    }
}

void deleteHeap(int number) {
    int pos = 0;
    for (int i = 1; i <= heapLen; ++i) {
        if (heap[i] == number) {
            pos = i;
            break;
        }
    }
    swap(heap[pos], heap[heapLen]);
    --heapLen;
    for (int i = 1; i <= heapLen; ++i) {
        updateHeap(i);
    }
}

void insertHeap(int number) {
    heap[++heapLen] = number;
    updateHeap(heapLen);
}

int main() {
    int n;
    fin >> n;
    for (int i = 1; i <= n; ++i) {
        int op;
        fin >> op;
        if (op == 1) {
            fin >> a[++aLen];
            insertHeap(a[aLen]);
        } else if (op == 2) {
            int pos;
            fin >> pos;
            deleteHeap(a[pos]);
        } else {
            fout << heap[1] << '\n';
        }
    }
    return 0;
}