Cod sursa(job #2258494)

Utilizator gabib97Gabriel Boroghina gabib97 Data 11 octombrie 2018 16:04:32
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <bits/stdc++.h>

#define N 40
#define CONF 132000
using namespace std;

int n, z[N], g, min_trucks[CONF], min_last[CONF];
vector<int> conf[N]; // conf[i]: configurations having i blocks

int main() {
    freopen("zebughil.in", "r", stdin);
    freopen("zebughil.out", "w", stdout);

    for (int q = 0; q < 3; q++) {
        cin >> n >> g;

        int i, j, m, mt, ml;
        for (i = 0; i < n; i++) cin >> z[i];

        int C = 1 << n, nr;
        for (i = 1; i < C; i++) {
            nr = 0;
            int aux = i;
            do nr++; while (aux &= aux - 1);
            conf[nr].push_back(i);
        }

        min_trucks[0] = 1;
        min_last[0] = 0; // one free truck
        for (i = 1; i <= n; i++)
            for (int x : conf[i]) {
                min_trucks[x] = n;

                for (j = 0; j < n; j++)
                    if (x & (1 << j)) {
                        m = min_trucks[x ^ (1 << j)];
                        if (min_last[x ^ (1 << j)] + z[j] <= g)
                            mt = m, ml = min_last[x ^ (1 << j)] + z[j];
                        else
                            mt = m + 1, ml = z[j];

                        if (min_trucks[x] > mt || (min_trucks[x] == mt && min_last[x] > ml)) {
                            min_trucks[x] = mt;
                            min_last[x] = ml;
                        }
                    }
            }

        for (i = 1; i <= n; i++) conf[i].clear();
        cout << min_trucks[C - 1] << '\n';
    }
    return 0;
}