Cod sursa(job #2353059)

Utilizator roberthostiucHostiuc Robert Gabriel roberthostiuc Data 23 februarie 2019 20:36:06
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 1005

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

struct obiect{
    int greutate, valoare;
};

int n, Gmax;
obiect O[NN];

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

int main(){
	assert(fin >> n >> Gmax );
	for(int i=1 ; i<=n ; ++i)
		assert(fin >> O[i].greutate >> O[i].valoare);
	sort (O + 1 , O + n + 1, fcmp);
    int G = 0;
    int 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;
    }
    fout << V;
	return 0;
}