Cod sursa(job #379812)

Utilizator indestructiblecont de teste indestructible Data 4 ianuarie 2010 00:01:45
Problema Lapte Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <stdio.h>
#include <string.h>
#define NMAX 101
int n,l,A[NMAX],B[NMAX],best[NMAX][NMAX],rez_f,ant[NMAX][NMAX];
char marc[NMAX];
void read()
{
	scanf("%d%d",&n,&l);
	int i;
	for (i=1; i<=n; i++)
		scanf("%d%d",&A[i],&B[i]);
}
inline int max(int a,int b)
{
	return a>b ? a : b;
}
void init()
{
	memset(marc,0,sizeof(marc));
	int i,j;
	for (i=0; i<NMAX; i++)
		for (j=0; j<NMAX; j++)
			best[i][j]=0;
}
inline int ok(int x)
{
	init();
	int i,j,k,val;
	marc[0]=1;
	for (i=1; i<=n; i++)
		for (j=l; j>=0; j--)
			for (k=0; k<=x/A[i] && k<=j; k++)
				if (marc[j-k])
				{
					val=x-k*A[i];
					if (best[i][j]<best[i-1][j-k]+val/B[i])
					{
						best[i][j]=best[i-1][j-k]+val/B[i];
						marc[j]=1;
						ant[i][j]=k;
					}
					best[i][j]=max(best[i][j],best[i-1][j-k]+val/B[i]);
				}
	if (best[n][l]>=l)
		return 1;
	return 0;
}
int cbin()
{
	int i=0,step=(1<<15);
	for (i=0; step; step>>=1)
		if (!ok(i+step))
			i+=step;
	return ++i;
}
void reconst(int x,int act)
{
	if (x==0)
		return ;
	int val=rez_f-ant[x][act];
	reconst(x-1,act-ant[x][act]);
	printf("%d %d\n",ant[x][act],val/B[x]);
}
int main()
{
	freopen("lapte.in","r",stdin);
	freopen("lapte.out","w",stdout);
	read();
	rez_f=cbin();
	if (ok(rez_f))
	{
		printf("%d\n",rez_f);
		reconst(n,l);
	}
	return 0;
}