Cod sursa(job #586280)

Utilizator darrenRares Buhai darren Data 30 aprilie 2011 14:21:28
Problema Fabrica Scor 70
Compilator cpp Status done
Runda Algoritmiada 2011, Runda Finală, Clasele 5-9 Marime 1.73 kb
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

typedef unsigned int uint;

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

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

bool ok1(int time)
{
    int total = 0;
    for (int i = 1; i <= Na; ++i)
        total += time / T1[i];
    return (total >= N);
}
bool ok2(int 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];

    int step = 1 << 30 - 1; step *= 2;
    for (Ta = step + 1; 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 = 1 << 30 - 1; step *= 2;
    for (Tb = step + 1; step; step >>= 1)
        if (Tb - step >= 1 && ok2(Tb - step))
            Tb -= step;

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

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