Cod sursa(job #2589694)

Utilizator MohamedXXXhaide sarpili MohamedXXX Data 26 martie 2020 18:35:50
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
using namespace std;
#define NN 5005

struct obiect{
    int greutate, valoare;
};

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

int n, Gmax;
obiect O[NN];

bool fcmp(obiect A, obiect B)
{
    return A.valoare * B.greutate > A.greutate * B.valoare;
}

int main(){
    in >> n >> Gmax ;
    for(int i=1 ; i<=n ; ++i)
       in >> O[i].greutate >> O[i].valoare;
    sort (O + 1 , O + n + 1, fcmp);
    int G = 0;
    double V = 0;
    int i = 1;
    while( i <= n)
    {
        if(G + O[i].greutate <= Gmax)
        {
            G += O[i].greutate;
            V += O[i].valoare;
            i ++;
        }
        else
            if(Gmax - G > 0)
            {
                int x = Gmax - G;
                double factor = 1.0 * x / O[i].greutate;
                G = Gmax;
                V += factor * O[i].valoare;
                i++;
            }
            else
                i = n + 1;
    }
    out << V;
    return 0;
}