Cod sursa(job #132950)

Utilizator mgntMarius B mgnt Data 6 februarie 2008 23:27:17
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream sin  ("zebughil.in");
ofstream sout ("zebughil.out");

int const MAXN = 17;
int best[1<<MAXN], minl[1<<MAXN];

static void test () {
  int n, g, z[MAXN];
  int i, j, k, m, vbest, vmin;
  
  sin >> n >> g;
  for(i=0;i<n;i++) sin >> z[i];
  
  best[0] = 0; minl[0] = 0;
  for(i=1, m = 1 << n; i < m; i ++) {
    best[i]=n; minl[i]=0;
    for(j=0;j<n;j++) {
      if(0 != (i & (1<<j))) {
        k = i & ~(1<<j);
        if(minl[k]>=z[j]) {
          vmin = minl[k] - z[j];
          vbest = best[k];
        } else {
          vmin = g - z[j];
          vbest = 1 + best[k];
        }
        if((vbest < best[i]) || ((vbest == best[i]) && (vmin > minl[i]))) {
          minl[i] = vmin;
          best[i] = vbest;
        }
      }
    }
  }
  sout << best[-1 + (1<<n)] << endl;
}

int main () { test(); test (); test (); }