Cod sursa(job #3266872)

Utilizator BledeaAlexBledea Alexandru BledeaAlex Data 10 ianuarie 2025 18:29:56
Problema Zebughil Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#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;
}