Cod sursa(job #2710186)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 22 februarie 2021 08:50:33
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

class data_type {
    public:
        int cnt, w;
};

const int INF = 1e16L;

void test_case() {
    int N, G;
    fin >> N >> G;
    vector<int> a(N);
    for(int &x : a)
        fin >> x;
    vector<data_type> dp(1 << N, {INF, INF});
    dp[0] = data_type{1, 0};
    for(int mask = 1; mask < (1 << N); ++mask)
        for(int i = 0; i < N; ++i)
            if(mask & (1 << i)) {
                data_type prv = dp[mask ^ (1 << i)];
                if(prv.w + a[i] > G) {
                    if(dp[mask].cnt > prv.cnt + 1 || (dp[mask].cnt == prv.cnt + 1 && a[i] < dp[mask].w)) {
                        dp[mask].cnt = prv.cnt + 1;
                        dp[mask].w = a[i];
                    }
                }
                else
                    if(dp[mask].cnt > prv.cnt || (dp[mask].cnt == prv.cnt && prv.w + a[i] < dp[mask].w)) {
                        dp[mask].cnt = prv.cnt;
                        dp[mask].w = prv.w + a[i];
                    }
            }
    fout << dp[(1 << N) - 1].cnt << '\n';
}

int32_t main() {
    for(int tc = 0; tc < 3; ++tc)
        test_case();
}