Cod sursa(job #1676338)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 5 aprilie 2016 20:41:37
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include<fstream>
#define DIM 132000
using namespace std;
int n, i, j, g, x, y, t;
int d[DIM], e[DIM], v[20];
ifstream fin("zebughil.in");
ofstream fout("zebughil.out");
int main(){
    for(t = 3; t; t--){
        fin>> n >> g;
        for(i = 0; i < n; i++){
            fin>> v[i];
        }
        for(i = 1; i < (1 << n); i++){
            d[i] = 1000;
            e[i] = 0;
            for(j = 0; j < n; j++){
                if( ( (i >> j) & 1) == 1 ){
                    if(e[i - (1 << j)] >= v[j]){
                        x = d[i - (1 << j)];
                        y = e[i - (1 << j)] - v[j];
                    }
                    else{
                        x = d[i - (1 << j)] + 1;
                        y = g - v[j];
                    }
                    if(x < d[i]){
                        d[i] = x;
                        e[i] = y;
                    }
                    else{
                        if(x == d[i] && y > e[i]){
                            e[i] = y;
                        }
                    }
                }
            }
        }
        fout<< d[ (1 << n) - 1] <<"\n";
    }
    return 0;
}