L2-003 月饼

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, need;
    cin >> n >> need;
    vector<double> have(n);
    vector<double> price(n);
    for (int i = 0; i < n; i++) {
        cin >> have[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> price[i];
    }
    vector<tuple<double, double, int>> up(n);
    for (int i = 0; i < n; i++) {
        up[i] = {price[i] / have[i], have[i], i};
    }
    sort(up.begin(), up.end(), [&](tuple<double, double, int> x, tuple<double, double, int> y) {
         return get<0>(x) > get<0>(y); 
    });

    double res = 0;
    for (int i = 0; i < up.size(); i++) {
        if (need >= get<1>(up[i])) {
            res += 1.0 * price[get<2>(up[i])];
            need -= get<1>(up[i]);
        } else {
            res += get<0>(up[i]) * 1.0 * need;
            need = 0;
        }
    }

    cout << fixed << setprecision(2) << res;

    return 0;
}
//4.16
//4.8
//
posted @   Xxaj5  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2020-03-14 多数元素 (3.12 leetcode每日打卡)
2020-03-14 (Good topic)回文数(3.13 leetcode每日打卡)
2020-03-14 (Good topic)贪心+二分查找:最长上升子序列(3.14 leetcode每日打卡)
2020-03-14 递归与分治思想:八皇后问题 (在此用递归) (回溯算法的典型题)
2020-03-14 递归与分治思想:汉诺塔(递归 && 分治思想)
2020-03-14 递归与分治思想:治思想 && 折半查找法(迭代 && 递归)
2020-03-14 递归与分治思想:n的阶乘 && 逆序任意长度字符串(递归)
点击右上角即可分享
微信分享提示