1020 月饼

简单贪心算法。

把月饼按单价递减的顺序排列。

需求一定时,每次售出单价最高的月饼存量.

#include"iostream"
#include<algorithm>
using namespace std;

struct Moocake {
    double store,sale,price;
} m[2000];

bool cmp(const Moocake& a,const Moocake& b) {
    return a.price > b.price;
}

int main() {
    int n;
    double demand;
    cin>>n>>demand;
    for(int i = 0; i < n; ++i)
        cin>>m[i].store;
    for(int i = 0; i< n; ++i) {
        cin>>m[i].sale;
        m[i].price = m[i].sale/m[i].store;
    }
    sort(m,m+n,cmp);
    double ans = 0;
    for(int i = 0; i < n; ++i) {
        if(demand >= m[i].store) {
            demand-=m[i].store;
            ans += m[i].sale;
        } else {
            ans += m[i].price*demand;//最后需求量少于库存量
            break;
        }    
    }
    printf("%.2f",ans);
    return 0;
}

 

posted @ 2020-02-16 12:53  tangq123  阅读(123)  评论(0编辑  收藏  举报