Pagini recente » Cod sursa (job #779334) | Cod sursa (job #496234) | Cod sursa (job #2677243) | Cod sursa (job #316497) | Cod sursa (job #567127)
Cod sursa(job #567127)
#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;
}