-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfrac_knapsack.cpp
More file actions
33 lines (31 loc) · 758 Bytes
/
frac_knapsack.cpp
File metadata and controls
33 lines (31 loc) · 758 Bytes
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
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
using namespace std;
bool comp(pll a, pll b) {
float ratio1 = float(a.first)/float(a.second);
float ratio2 = float(b.first)/float(b.second);
return ratio1 > ratio2;
}
int main() {
ll i,value,weight,n,w;
cin>>n>>w;
vector<pll > ratio;
for(i=0;i<n;i++) {
cin>>value>>weight;
ratio.push_back(make_pair(value,weight));
}
sort(ratio.begin(), ratio.end(), comp);
float res = 0.0;
i = 0;
while(i<n && w-ratio[i].second >= 0) {
res += ratio[i].first;
w -= ratio[i].second;
i++;
}
if(i<n) {
res += (float(ratio[i].first)/float(ratio[i].second))*w;
}
cout<<res<<endl;
return 0;
}