Pagini recente » Cod sursa (job #3282426) | Cod sursa (job #2220710) | Cod sursa (job #1357018) | Cod sursa (job #1831347) | Cod sursa (job #567149)
Cod sursa(job #567149)
#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 = 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 = 1; col <= N; ++ col)
best[conf][col] = inf;
best[0][1] = G;
for (int conf = 0; conf < (1 << N); ++ conf)
for (int col = 1; 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);
break;
}
}
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;
}