1070. Mooncake (25)

来源:互联网 发布:万达电商 大数据百家 编辑:程序博客网 时间:2024/06/09 19:41

计算出单价,对此进行排序,然后从大到小的筛选就好了

#include<iostream>#include<vector>#include<algorithm>#pragma warning(disable:4996)using namespace std;struct node {    double n, m, p;//数量,总金额,单价    bool operator<(const node that)const {        return this->p < that.p;    }};int main(){    int N, M;    vector<node> all;    cin >> N >> M;    all.resize(N);    for (int t = 0;t < N;t++)        scanf("%lf", &all[t].n);    for (int t = 0;t < N;t++)    {        scanf("%lf", &all[t].m);        all[t].p = all[t].m / all[t].n;    }    sort(all.rbegin(), all.rend());    double resault = 0;    for (auto x : all)    {        if (x.n > M)        {            resault += M*x.p;break;        }        if (x.n <= M)        {            resault += x.m;            M -= x.n;        }    }    printf("%.2lf\n", resault);}
0 0
原创粉丝点击