Cod sursa(job #567195)

Utilizator Catah15Catalin Haidau Catah15 Data 29 martie 2011 20:03:17
Problema Zebughil Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <iostream>
#include <fstream>

using namespace std;


#define maxN 20
#define maxN2 (1 << 19)
#define inf (1 << 30)


int best[maxN2][maxN], N, G;
int x[maxN];


int main()
{
	ifstream f("zebughil.in");
	ofstream g("zebughil.out");
	
	for (int i = 1; i <= 3; ++ i)
	{
		int sol = inf;
		
		f >> N >> G;
		
		for (int j = 1; j <= N; ++ j)
			f >> x[j];

		
		for (int conf = 0; conf < (1 << N); ++ conf)
			for (int col = 0; col <= N; ++ col)
				best[conf][col] = inf;

		
		best[0][1] = G;
		
		
		for (int conf = 0; conf < (1 << N); ++ conf)
			for (int col = 0; col <= N; ++ col)
			{	
				if (best[conf][col] == inf) continue;
				
				for (int bit = 0; bit < N; ++ bit)
				{	
					if ( (1 << bit) & conf ) continue;
					
					if (best[conf][col] >= x[bit + 1])
					{	
						best[conf + (1 << bit)][col] = best[conf][col] - x[bit + 1];
						
						if (conf + (1 << bit) == (1 << N) - 1)
							sol = min (sol, col);
					}
					else
					{	
						best[conf + (1 << bit)][col + 1] = G - x[bit + 1];
						
						if (conf + (1 << bit) == (1 << N) - 1)
							sol = min (sol, col + 1);
					}
				}
			}
		
		g << sol << '\n';
	}
	
	
	f.close();
	g.close();
	
	return 0;
}