Cod sursa(job #3234219)

Utilizator elbarosPetre Tudor elbaros Data 7 iunie 2024 23:11:30
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <algorithm>
using namespace std;

const int nmax = 18;

int n, v[nmax], g;
unordered_map<int, int> pos;
long long confWeight[1 << nmax], dp[1 << nmax];

int lsb(int val) {
    return (val ^ (val - 1)) & val;
}

int main() {
    ifstream in("zebughil.in");
    ofstream out("zebughil.out");

    for (int t = 3; t > 0; t--) {
        memset(confWeight, 0, sizeof(confWeight));
        memset(dp, 0, sizeof(dp));

        in >> n >> g;
        for (int i = 0; i < n; i++) {
            in >> v[i];
            pos[1 << i] = i;
        }

        for (int c = 1; c < (1 << n); c++) {
            confWeight[c] = confWeight[c - lsb(c)] + v[pos[lsb(c)]];
        }

        for (int c = 1; c < (1 << n); c++) {
            dp[c] = n;
            for (int subC = c; subC > 0; subC = ((subC - 1) & c)) {
                if (confWeight[subC] <= g) {
                    dp[c] = min(dp[c], dp[c ^ subC] + 1);
                }
            }
        }

        out << dp[(1 << n) - 1] << "\n";
    }

    return 0;
}