Cod sursa(job #907378)

Utilizator sebii_cSebastian Claici sebii_c Data 7 martie 2013 21:50:35
Problema Zebughil Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <cstdio>
#include <cstring>

#include <algorithm>

using namespace std;

const int MAXN = 18;
const int MAXS = (1 << 17);

long long max_weight;

int N, sol;
long long weight[MAXN];
bool mark[MAXN];

bool seen[MAXS][MAXN];

void doit(int trucks, long long w, int marked, int num)
{
    if (marked == N) {
        sol = min(sol, trucks);
    } else if (seen[num][trucks]) {
        return;
    } else if (w == 0) {
        seen[num][trucks] = true;
        doit(trucks + 1, max_weight, marked, num);
    } else {
        seen[num][trucks] = true;
        bool can_take = false;
        for (int i = 0; i < N; ++i) {
            if (!mark[i]) {
                if (weight[i] > w)
                    continue;
                can_take = true;
                mark[i] = true;
                doit(trucks, w - weight[i], marked + 1, num + (1 << i));
                mark[i] = false;
            }
        }
        if (!can_take)
            doit(trucks + 1, max_weight, marked, num);
    }
}

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

    int t = 3;
    while (t--) {
        scanf("%d %lld", &N, &max_weight);
        for (int i = 0; i < N; ++i) {
            mark[i] = false;
            scanf("%lld", &weight[i]);
        }

        memset(seen, false, sizeof(seen));

        sol = (int)(2e9);
        doit(1, max_weight, 0, 0);
        printf("%d\n", sol);
    }

    return 0;
}