Cod sursa(job #586970)

Utilizator AndreyPAndrei Poenaru AndreyP Data 3 mai 2011 17:23:37
Problema Fabrica Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.33 kb
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <set>
#include <algorithm>
using namespace std;
#define N 100010
#define ui unsigned int
#define pii pair< ui,ui >
#define fs first
#define sc second
#define mp make_pair

int n,nra,nrb;
ui ca[N],cb[N];
int h[N],nh;
ui v[N],t[N];
multiset< ui > s;
const ui zero = 0;

inline void citire() {
    scanf("%d%d%d",&n,&nra,&nrb);

    for(int i=1; i<=nra; ++i) {
        scanf("%u",&ca[i]);
        t[i] = ca[i];
        h[i] = i;
    }

    for(int i=1; i<=nrb; ++i)
        scanf("%u",&cb[i]);
}

inline int left_son(int x) {
    return (x<<1);
}
inline int right_son(int x) {
    return ((x<<1)+1);
}
inline int father(int x) {
    return (x>>1);
}

inline void upheap(int k) {
    int key = h[k];
    while(k>1 && t[key]<t[h[father(k)]]) {
        h[k] = h[father(k)];
        k = father(k);
    }
    h[k] = key;
}

inline void downheap(int k) {
    int son;
    do {
        son = 0;
        if(left_son(k)<=nh) {
            son = left_son(k);
            if(right_son(k)<=nh && t[h[right_son(k)]]<t[h[son]])
                son = right_son(k);
            if(t[h[son]]>=t[h[k]])
                son = 0;
        }

        if(son) {
            swap(h[k],h[son]);
            k = son;
        }
    }while(son);
}

inline void rezolvaa() {
    nh = nra;
    for(int i=(nra/2)+(nra&1); i>0; --i)
        downheap(i);

    for(int i=1; i<=n; ++i) {
        v[i] = t[h[1]];
        //printf("%d\n",h[1]);
        t[h[1]] += ca[h[1]];
        downheap(1);
    }
}

inline bool vezi(ui m) {
	s.clear();
	s.insert(v+1,v+n+1);
	ui x;
	multiset< ui >::iterator it;
	for(int i=1; i<=nrb && !s.empty(); ++i) {
		x = 0;
		while(x+cb[i]<=m) {
			it = s.lower_bound(x);
			if(it==s.end())
				break;
			x = (*it)+cb[i];
			if(x>m)
				break;
			s.erase(it);
		}
	}

	if(s.empty())
		return true;
	return false;
}

inline void rezolvab() {
	sort(cb+1,cb+nrb+1);
    ui p=v[n]+1,u=UINT_MAX,pok=u,m;

    //if(vezi(u)==false)
    //    exit(4);
    while(p<=u) {
        m = p + ((u-p)/2);

        if(vezi(m)) {
            pok = m;
            u = m-1;
        } else
            p = m+1;
    }

    printf("%u %u\n",v[n],pok);
}

int main() {
    freopen("fabrica.in","r",stdin);
    freopen("fabrica.out","w",stdout);

    citire();
    rezolvaa();
    printf("%u %u\n",v[n],v[n]+1);
    return 0;
    rezolvab();

    return 0;
}