关于背包问题的进一步感知
#include <iostream> #include <algorithm> using namespace std; const int maxn = 100005; struct three{ double w; double v; double p; }s[maxn]; bool cmp(three a,three b){ return a.p>b.p; } int main(){ int n; double m; cin>>n>>m; for(int i=0;i<n;i++){ cin>>s[i].w>>s[i].v; s[i].p=s[i].v/s[i].w; } sort(s,s+n,cmp); double sum=0.0; for(int i=0;i<n;i++){ if(m>s[i].w){ m-=s[i].w; sum+=s[i].v; } else{ sum+=m*s[i].p; break; } } cout<<sum<<endl; return 0; }
本文来自博客园,作者:小白兔奶糖, 侵权必究!!!