Cod sursa(job #287424)

Utilizator ScrazyRobert Szasz Scrazy Data 24 martie 2009 21:01:58
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define ll long long
#define inf 0x3f3f3f3f

const int maxn = 18;
//const int maxg = ;

int dp[1<<maxn];
int h[1<<maxn];
ll cost[maxn];
ll G;
int n;
int ares = inf;

#define isSet(mask, i) ((mask&(1<<(i))))
#define unSet(mask, i) ((mask^(1<<(i))))

void read()
{
    scanf("%d%lld\n", &n, &G); 
    for (int i = 0; i < n; ++i) scanf("%lld", &cost[i]);
}

void solve()
{
    h[0] = 0;
    dp[0] = 0;

    for (int i = 1; i < (1<<n); ++i)
    {
	for (int j = 0; j < n; ++j)
	    if (isSet(i, j))
	    { 
		ll mask = unSet(i, j);
		if (h[mask] >= cost[j] && (dp[i] > dp[mask] || (dp[i] == dp[mask] && h[i] < h[mask] - cost[j])))
		{
		    dp[i] = dp[mask];
		    h[i] = h[mask] - cost[j];
		}
		else if (dp[i] > dp[mask] + 1 || (dp[i] == dp[mask] + 1 && h[i] < G - cost[j]))
		{
		    dp[i] = dp[mask] + 1;
		    h[i] = G - cost[j];
		}
	    }
    }	
}

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

    for (int test = 0; test < 3; ++test)
    {
	read();
	memset(dp, 0x3f, sizeof(dp)); 
	solve();
	printf("%d\n", dp[(1<<n) - 1]);
    }

    return 0;
}