L2-003 月饼

贪心的从单价最高的开始买。注意正数不是正整数!!!

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e3 + 10;
struct Node 
{
   double price; // 单价
   int id; //属于第几个物品
};
double c[N], w[N]; // 正数不是正整数
vector<Node> arr;
bool cmp(Node &a, Node &b) 
{
    //小数比较不能直接 !=,因为小数有误差,但是这题好像不这样写也可以
    if(fabs(a.price - b.price) > 1e-6) return a.price > b.price;
    return a.id > b.id;
}
int main()
{
    double n, d;
    cin >> n >> d;
    for(int i = 1; i <= n; i ++) cin >> c[i];
    for(int i = 1; i <= n; i ++) cin >> w[i];
    for(int i = 1; i <= n; i ++) 
    {
        double price = w[i] / c[i]; // 计算单价
        arr.push_back({price, i});
    }
    double res = 0;
    sort(arr.begin(), arr.end(), cmp); // 排序
    for(int i = 0; i < arr.size(); i ++)
    {
        double price = arr[i].price;
        int id = arr[i].id;
        res += min(c[id], d) * price;
        d -= min(c[id], d);
        if(d <= 0) break;
    }
    printf("%.2lf", res);
    return 0;
}
相关推荐
做cv的小昊5 分钟前
【World Model】π0.5:a Vision-Language-Action Model with Open-World Generalization
人工智能·算法·机器学习·大模型·多模态·vla·世界模型
لا معنى له33 分钟前
World-In-World: World Models in a Closed-Loop World
算法
Logic1012 小时前
C语言/数据结构位运算题解:异或XOR找出多任务下载器中的“独特下载速度“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
Discipline10293 小时前
尼泊尔河流流量:天气驱动的预测与高流量风险
人工智能·算法
XuCoder3 小时前
改一个数,右边全得重算,这题怎么扛住两万次查询
算法
小羊没烦恼!3 小时前
在Scrum中实施敏捷建模
java·开发语言·windows·算法·c#
weixin_307779133 小时前
基于睿擎工业开发平台的预训练视觉模型轻量化适配与低代码部署优化
开发语言·算法
hold?fish:palm4 小时前
45 二叉树的右视图
开发语言·c++·算法
粤鼎恒业4 小时前
惠州工厂电子料回收厂家推荐:从交接单反推筛选标准
大数据·数据库·算法·硬件架构·硬件工程·pcb工艺·材料工程
小爷毛毛(卓寿杰)5 小时前
【Agent 意图识别】输出协议、评估与置信度
人工智能·算法·大模型·prompt·大语言模型·agent