Cod sursa(job #976703)

Utilizator manutrutaEmanuel Truta manutruta Data 23 iulie 2013 18:53:53
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

int n;
int a[200010], id[200010];
int k;

void operatie1 () {
    n++;
    f >> a[n];

    k++;
    id[n] = k;

    int i = n;
    while (a[i] < a[i / 2]) {
        int aux;
        aux = a[i];
        a[i] = a[i / 2];
        a[i / 2] = aux;

        aux = id[i];
        id[i] = id[i / 2];
        id[i / 2] = aux;

        i = i / 2;
    }
}

void operatie2 () {
    int nr;
    f >> nr;

    for (int i = 1; i <= n; i++) {
        if (id[i] == nr) {
            a[i] = a[n];
            id[i] = id[n];

            while ((a[i] > a[i * 2] || a[i] > a[i * 2 + 1]) && (i * 2 < n)) {
                if (a[i * 2] > a[i * 2 + 1] && i * 2 + 1 > 0)
                {
                    int aux;
                    aux = a[i];
                    a[i] = a[i * 2 + 1];
                    a[i * 2 + 1] = aux;

                    aux = id[i];
                    id[i] = id[i * 2 + 1];
                    id[i * 2 + 1] = aux;

                    i = i * 2 + 1;
                }
                else {
                    int aux;
                    aux = a[i];
                    a[i] = a[i * 2];
                    a[i * 2] = aux;

                    aux = id[i];
                    id[i] = id[i * 2];
                    id[i * 2] = aux;

                    i = i * 2;
                }
            }

            break;
        }
    }
    n--;
}

int main()
{
    int t;
    f >> t;
    for (int i = 1; i <= t; i++) {
        int ID;
        f >> ID;

        if (ID == 1) {
            operatie1();
        }

        if (ID == 2) {
            operatie2();
        }
        if (ID == 3) {
            g << a[1] << '\n';
        }
    }

    return 0;
}