【1070 25 贪心】 Mooncake

传送门

题意

给定\(n\)中月饼和要销售的数量\(m\),给出每种月饼的数量\(ton_{i}\),这些数量的总价格\(price_{i}\),求卖出\(m\)个最大的销售额是多少

数据范围

\(n\leq 1000\)
\(m\leq 500\)

题解

  • 按照单价降序排序
  • 优先选取单价高的销售即可

Code

#include<bits/stdc++.h>
using namespace std;
struct node{
    double cnt,p,avg;
    bool operator <(const node t) const{
        return avg>t.avg;
    }
};
int main(){
    int n,d; cin>>n>>d;
    vector<node>a(n);
    for(int i=0;i<n;i++) cin>>a[i].cnt;\
    for(int i=0;i<n;i++) cin>>a[i].p,a[i].avg=(double)a[i].p/a[i].cnt;
    sort(a.begin(),a.end());
    double c=0;
    for(int i=0;i<n;i++){
        if(a[i].cnt<d) c+=a[i].p;
        else {
            c+=a[i].avg*d;
            break;
        }
        d-=a[i].cnt;
    }
    printf("%.2lf",c);
}
posted @ 2021-03-02 20:58  Hyx'  阅读(44)  评论(0)    收藏  举报