Cod sursa(job #2295334)

Utilizator vladcoroian2001Vlad Coroian vladcoroian2001 Data 3 decembrie 2018 16:16:24
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <algorithm>

using namespace std;
ifstream fi("zebughil.in");
ofstream fo("zebughil.out");
const int NMAX=20;
int n,g,sum,aux,cost,prev_conf,z[NMAX];
pair <int,int> dp[(1<<NMAX)];
int main()
{
	for(int t=1;t<=3;t++)
	{
		fi>>n>>g;
		for(int i=0;i<n;i++)
			fi>>z[i];
		for(int conf=0;conf<=(1<<n)-1;conf++)
		{
			sum=0;
			for(int i=0;(1<<i)<=conf;i++)
				sum+=z[i];
			if(sum<=g)
				dp[conf]={1,sum};
			else
			{
				dp[conf]={18,0};
				for(int i=0;(1<<i)<=conf;i++)
					if((1<<i)&conf)
					{
						prev_conf=conf-(1<<i);
						cost=z[i]+dp[prev_conf].second;
						aux=dp[prev_conf].first;
						if(cost>g) cost=z[i],aux++;  
						if(dp[conf].first==aux && cost<dp[conf].second || dp[conf].first>aux)
						{
							dp[conf].first=aux;
							dp[conf].second=cost;
						}
					}
			}
		}
		fo<<dp[(1<<n)-1].first<<"\n";
	}
	fi.close();
	fo.close();
	return 0;
}