Cod sursa(job #1232387)

Utilizator assa98Andrei Stanciu assa98 Data 22 septembrie 2014 19:51:29
Problema Zebughil Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include <algorithm>
#include <vector>
#define pe pair<int, int>
#define mp make_pair
#define fi first
#define se second

using namespace std;

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

const int MAX_N = (1 << 17) + 100;

class cmp {
  public:
    inline bool operator ()(const pe &a, const pe &b) {
        return a.se < b.se;
    }
};

pe min(const pe &a, const pe &b) {
    if(a.fi < b.fi) {
        return a;
    }
    if(b.fi < a.fi) {
        return b;
    }
    if(a.se < b.se) {
        return a;
    }
    return b;
}

pe d[MAX_N];

int v[20];

void solve() {
    int n, g;
    fin >> n >> g;
    for(int i = 1; i <= n; i++) {
        fin >> v[i];
    }

    d[0] = mp(1, 0);
    for(int i = 1; i < (1 << n); i++) {
        d[i] = mp(20, 0);
    }

    for(auto it = 0; it < (1 << n); it++) {
        for(int i = 0; i < n; i++) {
            if(!(it & (1 << i))) {
                pe nou;
                if((long long)d[it].se + v[i] <= g) {
                    nou = mp(d[it].fi, d[it].se + v[i]);
                }
                else {
                    nou = mp(d[it].fi + 1, v[i]);
                }
                
                d[it | (1 << i)] = min(d[it | (1 << i)], nou);
            }
        }
    }

    fout << d[(1 << n) - 1].fi << '\n';
}

int main() { 
    for(int i = 1; i <= 3; i++) {
        solve();
    }
    return 0;
}