Cod sursa(job #2979793)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 15 februarie 2023 21:27:07
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("zebughil.in");
ofstream fout("zebughil.out");

const int nMax = 20;
int n, a[nMax];
long long G;
pair<int, long long>dp[(1 << 18)];
///dp[stare] = {nrMin de camioane, gMax ramasa in ultimul camion}
/// 010101110
///daca vreau sa pun un bloc : ori il pun in camionul cu gmax, ori in unul nou


int main()
{
    for(int t = 1; t <= 3; t++){
        fin >> n >> G;
        for(int i = 0; i < n; i++)
            fin >> a[i];
        for(int i = 0; i < (1 << n); i++)
            dp[i] = {n, 2e9};
        dp[0] = {1, G};
        for(int stare = 0; stare < (1 << n); stare++){
            for(int j = 0; j < n; j++)
                if(!(stare & (1 << j))){
                    int newStare = (stare | (1 << j));
                    if(dp[stare].second >= a[j]){
                        if(dp[stare].first < dp[newStare].first){
                             dp[newStare] = {dp[stare].first, dp[stare].second - a[j]};
                        }
                        else if(dp[stare].first == dp[newStare].first){
                            if(dp[stare].second - a[j] > dp[newStare].second)
                                dp[newStare] = {dp[stare].first, dp[stare].second - a[j]};
                        }
                    }

                    else{
                        if(dp[stare].first + 1 < dp[newStare].first){
                             dp[newStare] = {dp[stare].first + 1, G - a[j]};
                        }
                        else if(dp[stare].first + 1 == dp[newStare].first){
                            if(G - a[j] > dp[newStare].second)
                                dp[newStare] = {dp[stare].first + 1, G - a[j]};
                    }
                }

            }
        }
        fout << dp[((1 << n) - 1)].first << "\n";
    }
    return 0;
}