Cod sursa(job #585653)

Utilizator VmanDuta Vlad Vman Data 30 aprilie 2011 10:43:08
Problema Fabrica Scor 12
Compilator cpp Status done
Runda Algoritmiada 2011, Runda Finală, Open Marime 1.34 kb
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;

#define Pmax 50005
#define Nmax 200002

int N, NA, NB, TA, TB;
int A[Pmax], B[Pmax];
int T[Nmax];
priority_queue< pair<int,int> > H;

void timpA()
{
 int i, p, aux, nr;
 
 for (i=0, p=1; i<29; ++i, p<<=1);
 
 while (p)
	{
	 aux = TA + p;
	 p >>= 1;
	 for (i=nr=0; i<NA; ++i)
		 nr += aux / A[i];
	 if (nr<N) TA = aux;
	}
 ++TA;

 for (i=nr=0; i<NA; ++i)
	for (p=1; p*A[i]<=TA; ++p)
		T[nr++] = p*A[i];
 sort(T, T+nr);
}

void timpB()
{
 int i, p, aux, st = 0;
 pair<int,int> who;

 for (i=0, p=1; i<29; ++i, p<<=1);
 
 while (p)
	{
	 aux = TB + p;
	 p >>= 1;
	 //goleste heapul
	 while (! H.empty() ) H.pop();
	 //baga in heap
	 for (i=0; i<NB; ++i)
		 if (B[i] <= aux) H.push( make_pair(0, -B[i]) );
	 for (i=0; i<N; ++i)
		{
		 if (H.empty()) break;
		 who = H.top();
		 H.pop();
		 if (who.first - 2*who.second <= aux) H.push( make_pair(who.first - who.second, who.second) );
		}
	 if (i<N) TB = aux;
	}
}

int main()
{
 int i;

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

 scanf("%d %d %d", &N, &NA, &NB);
 for (i=0; i<NA; ++i)
	 scanf("%d", &A[i]);
 for (i=0; i<NB; ++i)
	 scanf("%d", &B[i]);
 
 timpA();
 printf("%d\n", TA);
 
 timpB();
 printf("%d\n", TA+TB);

 return 0;
}