Cod sursa(job #1814310)

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

int n, m, L, Heap[20000005];
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, y;
    while(x <= L){
        if(Heap[x * 2] < Heap[x * 2 + 1])
            y = x * 2;
        else
            y = x * 2 + 1;
        if(Heap[y] > Heap[x]) return ;
        if(y > L) return ;
        int aux = Heap[y];
        Heap[y] = Heap[x];
        Heap[x] = aux;
        x = y;
    }
}
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;
}