Cod sursa(job #3266883)

Utilizator BledeaAlexBledea Alexandru BledeaAlex Data 10 ianuarie 2025 18:39:32
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <bits/stdc++.h>

using namespace std;

const int N_MAX = 17, ST_MAX = 1 << N_MAX, INF = 1 << 30;
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 folosit 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()
{
    pair<int, int> nxt;

    dp[0] = make_pair(1, 0);
    for(int stare = 1, stare2, stareMaxima = 1 << N, maxBit = 1, popcount; stare < stareMaxima; stare++)
    {
        dp[stare] = make_pair(INF, INF);
        popcount = 0;
        for(int i = 0; i < maxBit; i++)
            if((stare & (1 << i)))
            {
                popcount++;

                stare2 = (stare ^ (1 << i));
                nxt = dp[stare2];
                if(dp[stare2].second + v[i] <= G)
                    nxt.second += v[i];
                else
                {
                    nxt.first++;
                    nxt.second = v[i];
                }

                if(nxt.first < dp[stare].first || (nxt.first == dp[stare].first && nxt.second < dp[stare].second))
                    dp[stare] = nxt;
            }

        if(popcount == 1)
            maxBit++;
    }

    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;
}