Pagini recente » Cod sursa (job #1519604) | Cod sursa (job #860247) | Cod sursa (job #958951) | Cod sursa (job #1714345) | Cod sursa (job #3266872)
#include <bits/stdc++.h>
#define pi pair<int, int>
#define f first
#define s second
using namespace std;
const int N_MAX = 17, ST_MAX = 1 << N_MAX;
int N, G;
int v[N_MAX];
pair<int, int> dp[ST_MAX]; /// dp[x] = {c, r} => folosind doar blocurile cu bit-ul corespunzator 1 in x obtinem
/// minim c camioane cu r spatiu ramas in ultimul camion
void SetInput(string name)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
(void)!freopen((name + ".in").c_str(), "r", stdin);
(void)!freopen((name + ".out").c_str(), "w", stdout);
}
void Solve()
{
dp[0] = make_pair(0, 0);
for(int stare = 1, stare2, stareMaxima = 1 << N; stare < stareMaxima; stare++)
{
dp[stare] = make_pair(N, 0);
for(int i = 0; i < N; i++)
if((stare & (1 << i)) != 0)
{
stare2 = stare ^ (1 << i);
if(dp[stare2].second >= v[i])
dp[stare] = min(dp[stare], make_pair(dp[stare2].first, dp[stare2].second - v[i]));
else
dp[stare] = min(dp[stare], make_pair(dp[stare2].first + 1, G - v[i]));
}
}
cout << dp[(1 << N) - 1].first << '\n';
}
void ReadInput()
{
for(int i = 0; i < 3; i++)
{
cin >> N >> G;
for(int j = 0; j < N; j++)
cin >> v[j];
Solve();
}
}
int main()
{
SetInput("zebughil");
ReadInput();
return 0;
}