-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathknapsack(fractional).txt
More file actions
43 lines (42 loc) · 893 Bytes
/
knapsack(fractional).txt
File metadata and controls
43 lines (42 loc) · 893 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
34
35
36
37
38
39
40
41
42
43
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll n,m;
cin>>n>>m;
ll w[n],p[n];
for(ll i=0;i<n;i++)
{
cin>>w[i];
}
for(ll i=0;i<n;i++)
{
cin>>p[i];
}
multimap<double,ll, greater<>> ma;
for(ll i=0;i<n;i++)
{
ma.insert(make_pair(double(double(p[i])/double(w[i])), i));
}
for(auto itr=ma.begin();itr!=ma.end();itr++)
{
//cout<<itr->first<<" "<<itr->second<<endl;
}
double f[100]={0};
for(auto itr=ma.begin();itr!=ma.end();itr++)
{
ll mini=min(m,w[itr->second]);
m=m-mini;
f[itr->second]=double(mini)/double(w[itr->second]);
if(m==0)
break;
}
double ans=0;
for(ll i=0;i<n;i++)
{
//cout<<f[i]<<endl;
ans+=p[i]*f[i];
}
cout<<ans<<endl;
}