Cod sursa(job #1077582)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 11 ianuarie 2014 14:44:54
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <memory.h>

#define Unu(conf, poz) ((conf) & (1 << (poz)))
#define Out(conf, poz) ((conf) ^ (1 << (poz)))

using namespace std;

ifstream fin("zebughil.in");
ofstream fout("zebughil.out");

const int Nmax = 17;
const int Cmax = (1 << Nmax);

int T = 3, N, G, confmax, DP[Cmax], V[Nmax];

int main()
{
    while(T--)
    {
        fin >> N >> G; confmax = (1 << N);
        memset(DP, -1, sizeof DP); DP[0] = G;

        for(int i = 0; i < N; i++)
            fin>>V[i];

        for(int i=1; i <= N; i++)
        {
            for(int conf = 0; conf < confmax; conf++)
            {
                if(DP[conf] != -1)
                    DP[conf] = G;
                for(int j = 0; j < N; j++)
                    if(Unu(conf, j))
                       if(DP[Out(conf, j)] - V[j] > DP[conf])
                            DP[conf] = DP[Out(conf, j)] - V[j];
            }

            if(DP[confmax - 1] >= 0)
            {
                fout<<i<<'\n';
                break;
            }
        }
    }
    return 0;
}