Cod sursa(job #378305)

Utilizator AndreyPAndrei Poenaru AndreyP Data 28 decembrie 2009 11:56:28
Problema Zebughil Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include<stdio.h>
#include<bitset>
#include<algorithm>
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
#define mp make_pair
#define N 20
int n,g;
int z[N];
pii a[131100];
bitset<131100> viz;
inline void citire()
{
	scanf("%d%d",&n,&g);
	for(int i=0; i<n; ++i)
		scanf("%d",&z[i]);
}
inline void rezolva()
{
	int lim=1<<n;
        viz.reset();
	viz[0]=1;
	a[0].fs=a[0].sc=0;
	int next;
	int aux1,aux2;

	for(int i=0; i<lim; ++i)
	{
		for(int j=1,nr=0; nr<n; j<<=1,++nr)
		{
			if((i&j)==1)
				continue;
			next=(i|j);
                        if(a[i].sc>=z[nr])
			{
				aux1=a[i].fs;
				aux2=a[i].sc-z[nr];
			}
			else
			{
				aux1=a[i].fs+1;
				aux2=g-z[nr];
			}
			if(viz[next]==1)
			{
				if(a[next].fs>aux1)
				{
					a[next].fs=aux1;
					a[next].sc=aux2;
				}
				else
				if(a[next].fs==aux1 && a[next].sc>aux2)
					a[next].sc=aux2;
			}
			else
			{
				a[next].fs=aux1;
				a[next].sc=aux2;
				viz[next]=1;
			}
		}
	}

	printf("%d\n",a[lim-1].fs);
}
int main()
{
	freopen("zebughil.in","r",stdin);
	freopen("zebughil.out","w",stdout);

	citire();
	rezolva();

	citire();
	rezolva();

	citire();
	rezolva();

	return 0;
}