Cod sursa(job #567127)

Utilizator Catah15Catalin Haidau Catah15 Data 29 martie 2011 19:08:33
Problema Zebughil Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <iostream>
#include <fstream>

using namespace std;


#define maxN 18
#define maxN2 (1 << 18)
#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;
		
		f >> N >> G;
		
		for (int j = 1; j <= N; ++ j)
			f >> x[j];
		
		for (int conf = 0; conf < (1 << N); ++ conf)
			for (int col = 1; col <= N; ++ col)
				best[conf][col] = inf;
		
		
		best[0][1] = G;
		
		bool ok = false;
		
		
		for (int conf = 0; conf < (1 << N) && !ok; ++ conf)
			for (int col = 1; col <= N && !ok; ++ 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])
					{	
						best[conf + (1 << bit)][col] = best[conf][col] - x[bit];
						
						if (conf + (1 << bit) == (1 << N) - 1)
						{
							sol = col;
							ok = true;
							break;
						}
					}
					else
					{	
						best[conf + (1 << bit)][col + 1] = G - x[bit];
						
						if (conf + (1 << bit) == (1 << N) - 1)
						{
							sol = col + 1;
							ok = true;
							break;
						}
					}
				}
			}
			
		g << sol << '\n';
	}
	
	
	f.close();
	g.close();
	
	return 0;
}