1070. Mooncake (25)

来源:互联网 发布:阿里云1m宽带 编辑:程序博客网 时间:2024/06/07 15:27

传送门:https://www.patest.cn/contests/pat-a-practise/1070

AC代码  背包问题,按照最高单价排列.

#include <iostream>#include <vector>#include <stdio.h>#include <algorithm>using namespace std;struct Node{    double weight,price;};int main(){    int n;    double profit = 0,total;    scanf("%d %lf",&n,&total);    vector<Node>ans(n);    for (int i=0; i<n; ++i)        scanf("%lf",&ans[i].weight);    for (int i=0; i<n; ++i)        scanf("%lf",&ans[i].price);    sort(ans.begin(), ans.end(), [](Node a,Node b){        double p1 = a.price/a.weight;        double p2 = b.price/b.weight;        return p1>p2;    });    for (int i=0; i<n; ++i) {        if(ans[i].weight<=total){            profit+=ans[i].price;            total = total - ans[i].weight;        }        else        {            profit+=total*ans[i].price/ans[i].weight;            total = 0;        }        if(total==0)            break;    }    printf("%.2lf\n",profit);    return 0;}


0 0
原创粉丝点击