Cod sursa(job #673854)

Utilizator informatician28Andrei Dinu informatician28 Data 4 februarie 2012 23:38:53
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include<fstream>
#include<algorithm>
#define DIM 5000

using namespace std; 
ifstream in("rucsac.in"); 
ofstream out("rucsac.out"); 

int N, G, castig_total; 
struct obiect
{
	int weight, profit; 
}O[DIM]; 

bool comp(const obiect &unu, const obiect &doi)
{
	return (unu.profit/unu.weight)>(doi.profit/doi.weight);
}
int main()
{
	int i, local_profit, local_weight, gasit = 0;
	
	in >> N >> G; 
	for(i = 1; i <= N; i++) 
		in >> O[i].weight >> O[i].profit; 
	
	sort(O+1, O+N+1, comp);
	i = 1;
	local_profit = 0; 
    local_weight = 0;
    while( local_weight < G && i <= N && !gasit )
    {
		if( O[i].weight <= G - local_weight )
			{
				local_weight += O[i].weight; 
		        local_profit += O[i].profit;
		}				
			
			else {
				castig_total = local_profit + (G-local_weight)*(O[i].profit/O[i].weight);
				gasit = 1;
			}
	i++;			
	}
	
	out << castig_total;
}