Cod sursa(job #312415)

Utilizator FlorianFlorian Marcu Florian Data 5 mai 2009 21:51:06
Problema Zebughil Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include<cstdio>
#include<string>
using namespace std;
int bst[1<<19 + 2][20];
int N, T;
int gr[20], G;
void dinamica()
{
	int i,j,poz, tmp,k;
	for(i=0;i<(1<<N);++i) for(j=0;j<=N;++j) bst[i][j] = -1;
	bst[0][0] = 0;
	for(i = 0; i<=(1<<N); ++i)
		for(j=0;j<N;++j)
			if(bst[i][j]!=-1)
				for(poz = 0; poz < N; ++poz)
			 	if((i&(1<<poz)) == 0)
				{
					k = i + (1<<poz);
					if( gr[poz] <= bst[i][j]) // poz incape in ultimul camion
					{
						tmp = bst[i][j] - gr[poz];
						if(tmp > bst[k][j]) bst[k][j] = tmp;
					}
					else
					{
						tmp = G - gr[poz]; // deschid camion nou
						bst[k][j+1] = tmp;
					}
				}
	for(i = 0; i<=N;++i )
		if(bst[(1<<N)-1][i] != -1) { printf("%d\n",i); return;}
}
int main()
{
	freopen("zebughil.in","r",stdin);
	freopen("zebughil.out","w",stdout);
	T = 3;
	int i;
	while(T--)
	{
		scanf("%d%d",&N,&G);
		for(i=0;i<N;++i) scanf("%d",&gr[i]);
		dinamica();
	}
	return 0;
}