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;
}
相关推荐
多恩Stone1 分钟前
【3D AICG 系列-8】PartUV 流程图详解
人工智能·算法·3d·aigc·流程图
铸人6 分钟前
再论自然数全加和-质数的规律
数学·算法·数论·复数
二年级程序员6 分钟前
一篇文章掌握“顺序表”
c语言·数据结构
历程里程碑1 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
你撅嘴真丑8 小时前
第九章-数字三角形
算法
uesowys8 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder9 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮9 小时前
AI 视觉连载1:像素
算法
智驱力人工智能9 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥10 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法