Cod sursa(job #585949)

Utilizator darrenRares Buhai darren Data 30 aprilie 2011 12:50:09
Problema Fabrica Scor 50
Compilator cpp Status done
Runda Algoritmiada 2011, Runda Finală, Clasele 5-9 Marime 1.74 kb
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

int N, Na, Nb;
long long T1[100002], T2[100002], Times[100002];
long long canBeg[100002];
long long Ta, Tb;

class compare
{
    public: bool operator () (const long long& i1, const long long& i2)
    {
        return canBeg[i1] < canBeg[i2];
    }
};
priority_queue<long long, vector<long long>, compare> P;

bool ok1(long long time)
{
    long long total = 0;
    for (int i = 1; i <= Na; ++i)
        total += time / T1[i];
    return (total >= N);
}
bool ok2(long long time)
{
    while (!P.empty()) P.pop();
    for (int i = 1; i <= Nb; ++i)
    {
        canBeg[i] = time - T2[i];
        P.push(i);
    }
    for (int i = N; i >= 1; --i)
        if (Times[i] <= canBeg[P.top()])
        {
            int aux = P.top(); P.pop();
            canBeg[aux] -= T2[aux];
            P.push(aux);
        }
        else return false;
    return true;
}

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

    fin >> N >> Na >> Nb;
    for (int i = 1; i <= Na; ++i)
        fin >> T1[i];
    for (int i = 1; i <= Nb; ++i)
        fin >> T2[i];

    long long step = 1LL << 32;
    for (Ta = step + 1LL; step; step >>= 1)
        if (Ta - step >= 1 && ok1(Ta - step))
            Ta -= step;

    for (int i = 1; i <= Na; ++i)
        for (int j = 1; j <= Ta / T1[i]; ++j)
            Times[++Times[0]] = T1[i] * j;
    sort(Times + 1, Times + Times[0] + 1);
    Times[0] = N;

    step = 1LL << 32;
    for (Tb = step + 1LL; step; step >>= 1)
        if (Tb - step >= 1 && ok2(Tb - step))
            Tb -= step;

    fout << Ta << ' ' << Tb;

    fin.close();
    fout.close();
}