Mai intai trebuie sa te autentifici.

Cod sursa(job #5567)

Utilizator m_dersidanDersidan Mihai m_dersidan Data 13 ianuarie 2007 11:25:39
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
# include <stdio.h>
# include <string.h>

# define  _fin  "zebughil.in"
# define  _fout "zebughil.out"

# define  maxn  18
# define  maxb  131099


int c[maxn], n, g;
int best[maxb][maxn];
int sol;


void readf()
{
	// read each test
	int i;
	for (scanf("%d %d", &n, &g), i=1; i<=n; i++)
		 scanf("%d", c+i);
		
	memset(best, 0xff, sizeof(best));
}

void solve()
{
	int i, j, k, to = (1<<n) - 1, aux, from;
	
	for (i=1, j=1; j<=n; i<<=1, j++)
		best[ i ][ 1 ] = g - c[j];
		
	for (i=1; i<=to; i++)
	{
		for (j=1; j<=n; j++)	// numarul de camioane necesare pentru a transporta si acest bloc
		{
			if ( best[i][j]!=-1 ) continue;
			for (k=1; k<=n; k++)
				if ( i & (1<<(k-1)) )	// adica se transporta blocul k
				{
					from = i ^ (1<<(k-1));
					if ( best[from][j]!=-1 )
						if ( best[from][j]-c[k] > best[i][j] )
							best[i][j] = best[from][j]-c[k];
						
					if ( best[from][j-1]!=-1 )	// incepem camion nou
						if ( g-c[k] > best[i][j] )
							best[i][j] = g-c[k];
				}
		}
	}
	
	for (j=1; j<=n; j++)
		if ( best[to][j] != -1 )
		{
			sol = j;
			break;
		}
}

int main()
{
	freopen(_fin, "r", stdin);
	freopen(_fout,"w", stdout);
	
	int i;
	
	for (i=1; i<=3; i++)
	{
		readf();
		solve();
		printf("%d\n", sol);
	}
	
	return 0;
}