Cod sursa(job #95301)

Utilizator the1dragonIonita Alexandru the1dragon Data 28 octombrie 2007 03:01:01
Problema Lapte Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include<stdio.h>
int speed[128][2], best[256], last[256][2], n, q, re[256][256][2], solutie[128][2];
bool vizitat[256];

int sol(int t)
{
	int i, j, k, a, b;
	for (i=0; i<256;  i++)		//sparge linia de cache
		best[i]=last[i][0]=last[i][1]=vizitat[i]=0;
	vizitat[0]=true;
	for (i=1; i<=n; i++)
		for (j=q; j>=0; j--)
		{			
			for (k=t; k>=0; k--)
			{
				a=k/speed[i][0];
				b=(t-k)/speed[i][1];
				if ((vizitat[j]) && (last[j][0]!=i) && (best[j+a]<best[j]+b))
				{
					best[j+a]=best[j]+b;
					vizitat[j+a]=true;
					last[j+a][0]=i;
				//	last[j+a][1]=k;
					re[j+a][best[j+a]][0]=i;
					re[j+a][best[j+a]][1]=k;
				}
				
			}
		}
	int max=0, s=0;
	for (i=0; i<=100; i++)
		if ((best[i+q]>=q) && (max<best[i+q]+i+q))
		{
			max=best[i+q]+i+q;
			s=i+q;
		}
	return s;
}

int main()
{
	freopen("lapte.in", "r", stdin);
	freopen("lapte.out", "w", stdout);
	int i, st=1, dr=100, mij;
	scanf("%d %d", &n, &q);
	for (i=1; i<=n; i++)
		scanf("%d %d", &speed[i][0], &speed[i][1]);
	while(st!=dr)
	{
		mij=(st+dr)/2;
		if (sol(mij))
			dr=mij;
		else
			st=mij+1;
	}
	printf("%d\n", st);
///*	//refacerea drumului
	int a=sol(st);
	int b=best[a], x, y;
	while ((a!=0)||(b!=0))
	{
	//	printf("%d %d\n", a, b);
		solutie[re[a][b][0]][0]+=x=re[a][b][1]/speed[re[a][b][0]][0];
		solutie[re[a][b][0]][1]+=y=(st-re[a][b][1])/speed[re[a][b][0]][1];
		a-=x;
		b-=y;
	}
	for (i=1; i<=n; i++)
		printf("%d %d\n", solutie[i][0], solutie[i][1]);
//*/
	return 0;
}