Cod sursa(job #585614)

Utilizator S7012MYPetru Trimbitas S7012MY Data 30 aprilie 2011 10:18:41
Problema Fabrica Scor 20
Compilator cpp Status done
Runda Algoritmiada 2011, Runda Finală, Clasele 10-12 Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <set>
#define DNR 50005
#define x first
#define y second
using namespace std;

typedef pair<int, int> per;//timp,proc
set<per> a,b;
int n,na,nb,ta[DNR],tb[DNR],ra,rb,tta,ttb;

int main()
{
    ifstream f("fabrica.in");
    ofstream g("fabrica.out");
    f>>n>>na>>nb;
    ra=rb=n;
    for(int i=1; i<=na; ++i) {
        f>>ta[i];
        a.insert(make_pair(ta[i],i));
    }
    for(int i=1; i<=nb; ++i) {
        f>>tb[i];
        b.insert(make_pair(tb[i],i));
    }
    per top;
    bool ok=0;

    top=*a.begin();
    a.erase(a.begin());
    --ra;
    tta=top.x;
    a.insert(make_pair(top.x+ta[top.y],top.y));
    ttb=tta+ta[top.y];
    top=*b.begin();
    b.erase(b.begin());
    b.insert(make_pair(ttb+tb[top.y],top.y));

    for(int i=2; i<=n; ++i) {
        if(ra) {
            top=*a.begin();
            a.erase(a.begin());
            --ra;
            tta=top.x;
            a.insert(make_pair(top.x+ta[top.y],top.y));
        }
        if(0==ra && 0==ok) {
            g<<tta<<' ';
            ok=1;
        }
        top=*b.begin();
        b.erase(b.begin());
        ttb=top.x;
        b.insert(make_pair(ttb+tb[top.y],top.y));
    }
    top=*b.begin();
    g<<ttb<<'\n';
    return 0;
}