Cod sursa(job #1099207)

Utilizator gaby1995Calin Gabriel Iulian gaby1995 Data 5 februarie 2014 17:22:38
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2 kb
#include <fstream>

#define Nmax 40

//TODO declara mask local cu 2^n
int n, mask[Nmax], w, et, ct;

struct reactor {
    int e,c;
} v[Nmax];

struct possible {
    int c;
    float ef;
} a[Nmax];

int next(int mask[]) {
    int i ;

    for (i = 0 ; i < n && mask[i]; i ++)
        mask[i] = 0;

    if (i < n) {
        mask[i] = 1;
        return 1;
    }
    return 0;
}

void print(int mask[]) {

    for (int i = 0 ; i < n ; i ++)
        printf("%d ", mask[i]);
    printf("\n");
}
int main()
{

    freopen("energii.in", "r", stdin);
    freopen("energii.out", "w", stdout);

    scanf("%d", &n);

    scanf("%d", &w);

    for (int i = 0 ; i < n ; i++) {
        scanf("%d %d", &v[i].e, &v[i].c);
    }

    fcloseall();


    int psl = -1;

// while (next(mask) && !ans) {
    while (next(mask)) {
      et=ct=0;

    for (int i = 0 ; i < n ; i ++)
        if (mask[i]) {
            et += v[i].e;
            ct += v[i].c;
            }



//    if (et >= w) {
//        printf("found with cost %d and energy %d \n",ct, et );
//        ans = true;
//    }

//    printf("{ %d %d } \n", et, ct);

    if (et >= w) {
//        printf("found with cost %d and energy %d \n",ct, et );
        psl++;
        a[psl].c = ct;
        a[psl].ef = (float)ct/et;


    }




}
//    if (ans == false)
//        printf("%d", -1);
//
//    for (int i = 0 ; i < psl ; i ++) {
//        printf("solutia %d, eficienta %f, cost %d \n",i ,a[i].ef, a[i].c);
//    }


//start bubblesorting

    int sorted = 0;
    possible aux;

    while (!sorted) {

        sorted = 1;
        for (int i = 0 ; i < n ; i ++)
            if (a[i].ef > a[i+1].ef) {
                aux = a[i];
                a[i] = a[i+1];
                a[i+1] = aux;
                sorted = 0;
            }
    }

//    printf("*** STARTED SORTING **** \n");
//
//    for (int i = 0 ; i < psl ; i ++) {
//        printf("solutia %d, eficienta %f, cost %d \n",i ,a[i].ef, a[i].c);
//    }
//
//    printf("*** FINAL RESULT **** \n");


    printf("%d", a[0].c);


    return 0;
}