Cod sursa(job #3257892)

Utilizator luc3lexa_Alexandrescu Luca luc3lexa_ Data 19 noiembrie 2024 20:11:02
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ifstream fin("zebughil.in");
ofstream fout("zebughil.out");

const int nmax = 18;
const int nmax2 = (1 << 17)+1;
const int inf = 20;

vector<pair<int,ll>> dp(nmax2,{inf,0});
vector<ll> cost(nmax,0);
ll n,g;

void solve(){
	fill(dp.begin(),dp.begin() + nmax2 + 1,make_pair(inf,0));
	fill(cost.begin() + 1,cost.begin() + 1 + nmax,0);
	
	fin  >> n >> g;
	for(int i = 1; i <=n; i++){
		fin >> cost[i];
	}
	
	dp[0].first = 1;
	
	for(ll mask = 0; mask <= (1 << n)-1;mask++){
		for(int j = 0; j <n; j++){
			if(!(mask & (1 << j))){
			
				ll valmin = dp[mask].first;
				ll curr_weight = dp[mask].second + cost[j+1];
				ll new_mask = (mask ^ (1LL << j));
				
				if(curr_weight > g){
					curr_weight = cost[j+1];
					valmin++;
				}
				
				if(valmin < dp[new_mask].first){
					dp[new_mask].first = valmin;
					dp[new_mask].second = curr_weight;
				}else if(valmin == dp[new_mask].first && curr_weight < dp[new_mask].second){
					dp[new_mask].second = curr_weight;
				}
			}
		}
	};
	
	fout << dp[(1 << n)-1].first << '\n';
}


int main(){
	//ios_base::sync_with_stdio(false);
	//cin.tie(NULL);cout.tie(NULL);
	
	for(int i = 1; i <= 3; i++){
		solve();
	}
	return 0;
}