Cod sursa(job #1824310)

Utilizator cella.florescuCella Florescu cella.florescu Data 7 decembrie 2016 18:16:06
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 17;

struct Stuff {
  int mintr, spleft;
  bool operator < (const Stuff &other) const {
    if (mintr == other.mintr)
      return spleft > other.spleft;
    return mintr < other.mintr;
  }
} dp[1 << MAXN];

inline Stuff minim(Stuff A, Stuff B) {
  if (A < B)
    return A;
  return B;
}

int z[MAXN];

int main()
{
    int n, g, aux;
    ifstream fin("zebughil.in");
    ofstream fout("zebughil.out");
    for (int t = 0; t < 3; ++t) {
      fin >> n >> g;
      for (int i = 0; i < n; ++i)
        fin >> z[i];
      dp[0] = {0, 0};
      for (int conf = 1; conf < (1 << n); ++conf) {
        dp[conf] = {MAXN + 1, -1};
        for (int i = 0; i < n; ++i)
          if ((1 << i) & conf) {
            aux = conf - (1 << i);
            if (dp[aux].spleft >= z[i])
              dp[conf] = minim(dp[conf], {dp[aux].mintr, dp[aux].spleft - z[i]});
            else
              dp[conf] = minim(dp[conf], {dp[aux].mintr + 1, g - z[i]});
          }
      }
      fout << dp[(1 << n) - 1].mintr << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}