Cod sursa(job #2401872)

Utilizator osiaccrCristian Osiac osiaccr Data 10 aprilie 2019 10:08:40
Problema Fabrica Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <bits/stdc++.h>
#define DEF 100010

using namespace std;

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

int n, nrA, nrB, masA[DEF], masB[DEF], finishA, finishB, globalTime, readyforB;

priority_queue < pair < int, int >, vector < pair < int, int > >, greater < pair < int, int > > > HA, HB, HBfree;

int main () {

    fin >> n >> nrA >> nrB;

    for (int i = 1; i <= nrA; ++ i) {
        fin >> masA[i];
        HA.push ({masA[i], i});
    }

    for (int j = 1; j <= nrB; ++ j) {
        fin >> masB[j];
        HBfree.push ({masB[j], j});
    }


    while (finishA < n) {

        auto top = HA.top ();
        HA.pop ();

        ++ finishA; ++ readyforB;
        globalTime = top.first;

        HA.push ({masA[top.second] + globalTime, top.second});


        while (HB.size () > 0 and HB.top ().first <= globalTime) {
            ++ finishB;
            auto newtop = HB.top ();
            HB.pop ();

            if (readyforB) {
                -- readyforB;
                HB.push ({masB[newtop.second] + globalTime, newtop.second});
            }
            else {
                HBfree.push ({masB[newtop.second], newtop.second});
            }
        }

        while (readyforB and HB.size () < n and HBfree.size () > 0) {
            auto freeMachine = HBfree.top ();
            HBfree.pop ();

            readyforB--;

            HB.push ({freeMachine.first + globalTime, freeMachine.second});
        }
    }

    fout << globalTime << " ";

    while (finishB < n) {

        auto top = HB.top ();
        HB.pop ();

        ++ finishB;
        globalTime = top.first;

        HB.push ({masB[top.second] + globalTime, top.second});

    }

    fout << globalTime;

    return 0;
}