Cod sursa(job #95296)

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

int sol(int t)
{
	int i, j, k, a, b;
	for (i=0; i<512;  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 (k=t; k>=0; k--)
		{
			a=k/speed[i][0];
			b=(t-k)/speed[i][1];
			for (j=q; j>=0; j--)
			{
				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;
				}
				
			}
		}
	for (i=0; i<=n; i++)
		if (best[i+q]>=q) return i+q;
	return 0;
}

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);
	int f=sol(st);
//	for (i=0; i<=f; i++)
//		printf("%d %d %d\n", best[i], last[i][0], last[i][1]);
///*	//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;
	}
//	int j;
//	for (i=0; i<=50; i++)
//	{	for (j=0; j<=50; j++)
//			printf("%d, %d  ", re[i][j][0], re[i][j][1]);
//		printf("\n");
//	}	
	for (i=1; i<=n; i++)
		printf("%d %d\n", solutie[i][0], solutie[i][1]);
//*/
	return 0;
}