forked from aayushi1499/Hacktoberfest2021-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractional_knapsack.cpp
74 lines (62 loc) · 1.41 KB
/
fractional_knapsack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
struct Item{
int value;
int weight;
};
//class implemented
/*
struct Item{
int value;
int weight;
};
*/
class Solution
{
public:
//Function to get the maximum total value in the knapsack.
double fractionalKnapsack(int W, Item arr[], int n)
{
// Your code here
vector<pair<double,int>> v;
for(int i=0;i<n;i++){
double x = (arr[i].value*1.0)/(arr[i].weight*1.0);
v.push_back({x,i});
}
sort(v.begin(), v.end(), greater<pair<double,int>>()); //STL predefined
int s=0;
double ans=0;
for(int i=0;i<v.size();i++){
if(s+ arr[v[i].second].weight< W){
ans += arr[v[i].second].value;
s += arr[v[i].second].weight;
}else{
double x=(W-s)*1.0;
ans += (x*v[i].first);
break;
}
}
return ans;
}
};
int main()
{
int t;
//taking testcases
cin>>t;
cout<<setprecision(2)<<fixed;
while(t--){
//size of array and weight
int n, W;
cin>>n>>W;
Item arr[n];
//value and weight of each item
for(int i=0;i<n;i++){
cin>>arr[i].value>>arr[i].weight;
}
//function call
Solution ob;
cout<<ob.fractionalKnapsack(W, arr, n)<<endl;
}
return 0;
}