B站头图景深对焦效果

请在图片上左右移动鼠标查看效果

关于背包问题的进一步感知

#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;
}

 

posted @ 2021-09-05 21:17  acmWang  阅读(17)  评论(0)    收藏  举报
返回顶端

2021-09-22 (星期三)
09:08:39 +08:00