Cod sursa(job #3235287)

Utilizator AndreiLeusteanLeustean Andrei AndreiLeustean Data 16 iunie 2024 19:46:20
Problema Interclasari Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.98 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("interclasari.in");
ofstream out("interclasari.out");

template <class T>
void minHeapInsert(T heap[], int &size, T x)
{
    int i = size + 1;
    size += 1;
    heap[i] = x;
    while (i != 1 && heap[i] < heap[i / 2])
    {
        swap(heap[i], heap[i / 2]);
        i /= 2;
    }
}

template <class T>
void minHeapify(T heap[], int size, int pos)
{
    int left = pos * 2;
    int right = pos * 2 + 1;
    int smallest;
    if (left <= size and heap[left] < heap[pos])
        smallest = left;
    else
        smallest = pos;
    if (right <= size and heap[right] < heap[smallest])
        smallest = right;
    if (smallest != pos)
    {
        swap(heap[pos], heap[smallest]);
        minHeapify(heap, size, smallest);
    }
}

template <class T>
T extractMin(T heap[], int &size)
{
    T minimum = heap[1];
    heap[1] = heap[size];
    size -= 1;
    minHeapify(heap, size, 1);
    return minimum;
}

template <class T>
void createMinHeap(T heap[], int size)
{
    for (int i = size / 2; i > 0; i--)
        minHeapify(heap, size, i);
}

template <class T>
void maxHeapInsert(T heap[], int &size, T x)
{
    int i = size + 1;
    size += 1;
    heap[i] = x;
    while (i != 1 && heap[i] > heap[i / 2])
    {
        swap(heap[i], heap[i / 2]);
        i /= 2;
    }
}

template <class T>
void maxHeapify(T heap[], int size, int pos)
{
    int left = pos * 2;
    int right = pos * 2 + 1;
    int greatest;
    if (left <= size and heap[left] > heap[pos])
        greatest = left;
    else
        greatest = pos;
    if (right <= size and heap[right] > heap[greatest])
        greatest = right;
    if (greatest != pos)
    {
        swap(heap[pos], heap[greatest]);
        maxHeapify(heap, size, greatest);
    }
}

template <class T>
T extractMax(T heap[], int &size)
{
    T maximum = heap[1];
    heap[1] = heap[size];
    size -= 1;
    maxHeapify(heap, size, 1);
    return maximum;
}

template <class T>
void createMaxHeap(T heap[], int size)
{
    for (int i = size / 2; i > 0; i--)
        maxHeapify(heap, size, i);
}

template <class T>
void heapSort(T vector[], int size)
{
    if (size != 1)
    {
        swap(vector[1], vector[size]);
        maxHeapify(vector, size - 1, 1);
        heapSort(vector, size - 1);
    }
}

template <class T>
void afisare(T v[], int size, ostream &out)
{
    for (int i = 1; i <= size; i++)
        out << v[i] << " ";
}

const int NMAX = 1e8;
int H[NMAX];

int main()
{
    // int n;
    // in >> n;
    // for (int i = 1; i <= n; i++)
    //     in >> v[i];
    // createMaxHeap(v, n);
    // heapSort(v, n);
    // afisare(v, n, out);
    // return 0;

    // int v[1000001];
    // long long a[100002];

    // long long n, A, B, C, D;
    // int k;
    // in >> n >> k >> A >> B >> C >> D;
    // a[1] = A;
    // for (int i = 2; i <= k; i++)
    // {
    //     a[i] = (B * a[i - 1] + C) % D;
    //     // cout<<a[i]<<" ";
    // }
    // long long nrant = a[k];
    // createMinHeap(a, k);
    // for (int i = k + 1; i <= n; i++)
    // {
    //     long long nrcurent = (B * nrant + C) % D;
    //     if (nrcurent > a[1])
    //     {
    //         extractMin(a, k);
    //         minHeapInsert(a, k, nrcurent);
    //     }
    //     nrant = nrcurent;
    // }

    // while (k != 0)
    // {
    //     out << extractMin(a, k) << " ";
    // }

    int nr;
    in >> nr;
    int suma = 0;
    int contor = 0;
    for (int i = 0; i < nr; i++)
    {
        int ni;
        in >> ni;
        suma += ni;
        while (ni)
        {
            int x;
            in >> x;
            contor++;
            H[contor] = x;
            ni -= 1;
        }
    }
    out << suma << endl;
    createMinHeap(H, contor);
    int m = contor;
    for (int i = 1; i <= m; i++)
    {
        out << extractMin(H, contor) << " ";
    }
    return 0;
}