Cod sursa(job #1999287)

Utilizator prodaniucpavelProdaniuc Pavel prodaniucpavel Data 10 iulie 2017 20:11:07
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int main()
{
    int nr_gen, pwr, best_cost = 30000, best_putere = 30000, putere = 0, cost = 0, best_gen = 0;
    float best_raport = 30000;
    bool found = false;

    fin >> nr_gen;
    fin >> pwr;

    int gen[nr_gen][3];

    for(int i = 0; i < nr_gen; i++) {
        fin >> gen[i][0];
        fin >> gen[i][1];

        gen[i][2] = 0;

        putere += gen[i][0];
    }

    if(putere < pwr) {
        cost = -1;
    } else {
        putere = 0;

        while(putere < pwr) {
            best_cost = 30000;

            for(int i = 0; i < nr_gen; i++) {
                if(gen[i][2] != 0) {
                    continue;
                }

                if(gen[i][0] + putere > pwr) {
                    if(gen[i][1] < best_cost or gen[i][1]/gen[i][0] < best_raport) {
                        best_putere = gen[i][0];
                        best_cost = gen[i][1];
                        best_gen = i;
                        found = true;
                    }

                } else if((float) gen[i][1]/gen[i][0] < best_raport and found == false) {
                    best_raport = (float) gen[i][1]/gen[i][0];
                    best_cost = gen[i][1];
                    best_putere = gen[i][0];
                    best_gen = i;
                }
            }

            gen[best_gen][2] = 1;
            putere += best_putere;
            cost += best_cost;

            best_raport = 30000;
        }
    }

    fout << cost;
}