Cod sursa(job #1814288)

Utilizator giotoPopescu Ioan gioto Data 23 noiembrie 2016 20:22:30
Problema Interclasari Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <cstdio>
using namespace std;

int n, m, L, Heap[3000001];
inline void push(int x){
    while(x / 2 && Heap[x] < Heap[x / 2]){
        int aux = Heap[x];
        Heap[x] = Heap[x / 2];
        Heap[x / 2] = aux;
        x /= 2;
    }
}
inline void pop(){
    Heap[1] = Heap[L];
    --L;
    int x = 1;
    if(Heap[x * 2] <= Heap[x * 2 + 1]){
        while(x * 2 <= L && Heap[x] > Heap[x * 2]){
            int aux = Heap[x];
            Heap[x] = Heap[x * 2];
            Heap[x * 2] = aux;
            x *= 2;
        }
        while(x * 2 + 1 <= L && Heap[x] > Heap[x * 2 + 1]){
            int aux = Heap[x];
            Heap[x] = Heap[x * 2 + 1];
            Heap[x * 2 + 1] = aux;
            x = x * 2 + 1;
        }
    }
    else{
        while(x * 2 + 1 <= L && Heap[x] > Heap[x * 2 + 1]){
            int aux = Heap[x];
            Heap[x] = Heap[x * 2 + 1];
            Heap[x * 2 + 1] = aux;
            x = x * 2 + 1;
        }
        while(x * 2 <= L && Heap[x] > Heap[x * 2]){
            int aux = Heap[x];
            Heap[x] = Heap[x * 2];
            Heap[x * 2] = aux;
            x *= 2;
        }
    }
}
int main()
{
    freopen("interclasari.in", "r", stdin);
    freopen("interclasari.out", "w", stdout);
    scanf("%d", &n);
    int x;
    for(int i = 1; i <= n ; ++i){
        scanf("%d", &m);
        for(int j = 1; j <= m ; ++j){
            scanf("%d", &x);
            ++L;
            Heap[L] = x;
            push(L);
        }
    }
    printf("%d\n", L);
    while(L > 0){
        printf("%d ", Heap[1]);
        pop();
    }
    return 0;
}