Cod sursa(job #673986)

Utilizator balakraz94abcd efgh balakraz94 Data 5 februarie 2012 13:07:08
Problema Zebughil Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <cstdio>
#include <bitset>
#include <algorithm>
#define infile "zebughil.in"
#define outfile "zebughil.out"
#define n_max 20
using namespace std;

int N, G;

bitset < n_max > uz;

int a[n_max], x[n_max];

int Best;



void rezolva()
{		

	int nrc = 1, sum = 0;
		
	for(int i=1;i<=N;++i)
		if(sum + x[i] <= G)
			sum += x[i];
		else
		{
			nrc++;
			sum = x[i];
		}
			
	Best = min(Best, nrc);
}


void back(int k)
{
	if(k > N)
	{
		rezolva();
		return;
	}
	
	for(int i=1;i<=N;i++)
		if(!uz[i])
		{
			x[k] = a[i];
			uz[i] = 1;
			back(k+1);
			uz[i] = 0;
		}
}



int main(void)
{
    freopen(infile, "r", stdin);
	freopen(outfile, "w", stdout);
    
	for(int t=1;t<=3;++t)
	{
		scanf("%d %d", &N, &G);
		
		for(int i=1;i<=N;++i)
			scanf("%d",&a[i]);
		
		Best = n_max;
		back(1);
		
		printf("%d\n",Best);
	}
    
    fclose(stdin);
	fclose(stdout);
    
    return 0;
}