Created
October 31, 2016 06:31
-
-
Save ahmedash95/59f5fa82a695f8e25806fa04cc721a35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<algorithm> | |
#include <cmath> | |
#include <limits> | |
#include <iomanip> | |
using namespace std; | |
struct Item{ | |
double value; | |
double weight; | |
double density; | |
}; | |
int main() | |
{ | |
int n,c; // c for Capacity | |
cin >> n >> c; | |
Item items[n]; | |
double v = 0, | |
w = 0; | |
for(unsigned int i =0; i < n;i++){ | |
cin >> v >> w; | |
Item tmp = { v , w , v/w }; | |
items[i] = tmp; | |
} | |
// sort items per density | |
for(int i = 0; i < n; i++){ | |
for(int j = 0; j < n; j++){ | |
if(items[i].density > items[j].density){ | |
Item tmp = items[i]; | |
items[i] = items[j]; | |
items[j] = tmp; | |
} | |
} | |
} | |
double benifit = 0; | |
int totalWeight = 0; | |
for(int i = 0; i < n; i++){ | |
Item item = items[i]; | |
int weight = item.weight + totalWeight; | |
if(weight <= c) | |
{ | |
totalWeight = weight; | |
benifit += item.value; | |
} else { | |
benifit += (c - totalWeight)/item.weight * item.value; | |
totalWeight = c; | |
} | |
} | |
cout << std::fixed << std::setprecision(4) << benifit << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment