Cod sursa(job #2055549)

Utilizator benjamin2205Zeic Beniamin benjamin2205 Data 3 noiembrie 2017 13:36:03
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("rucsac.in");
ofstream g ("rucsac.out");

int n, gMax;

struct obiect {
    int greutate;
    int profit;
    float raport;
};

obiect obiecte[5005];

void citire() {
    int profit;
    f >> n >> gMax;
    for (int i = 1; i <= n; ++i) {
        f >> obiecte[i].greutate;
        f >> obiecte[i].profit;
        obiecte[i].raport = (float)obiecte[i].profit/obiecte[i].greutate;
    }
}

bool compare(obiect leftValue, obiect rightValue) {
    return (leftValue.profit > rightValue.profit) ||
    ((leftValue.profit == rightValue.profit) &&
    (leftValue.raport > rightValue.raport));

}

void procesare() {
    int greutate = 0;
    float profit = 0;
    //sorteaza bine (primul obiect e 9)
    sort(obiecte + 1, obiecte + 1 + n, compare);
    for (int i = 1; i <= n; ++i) {
        if (greutate + obiecte[i].greutate <= gMax) {
            greutate += obiecte[i].greutate;
            profit += obiecte[i].profit;
            //cout << obiecte[i].greutate << ' ' << obiecte[i].profit << '\n';
        }
    }
    g << profit;
}

void afiseazaSir() {
    for (int i = 1; i <= n; ++i) {
        //cout << obiecte[i].greutate << ' ' << obiecte[i].profit << ' '
        //<< obiecte[i].raport << '\n';
    }
}

int main()
{
    citire();
    procesare();
    afiseazaSir();
    return 0;
}