L2-003 月饼 (25 分)

摘要
Title: L2-003 月饼 (25 分)
Tag: 贪心
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

L2-003 月饼 (25 分)

  • 题意

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。
    注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。

  • 思路

    因为月饼可分,所以可以用贪心的思想装满
    注意:月饼数和价值,都是浮点型

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    /*
    * @Author: NEFU AB-IN
    * @Date: 2022-04-19 17:35:45
    * @FilePath: \ACM\GPLT\L2-003.CPP
    * @LastEditTime: 2022-04-19 17:59:52
    */
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #define MP make_pair
    #define SZ(X) ((int)(X).size())
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(0); \
    cout.tie(0);
    #define DEBUG(X) cout << #X << ": " << X << endl;
    typedef pair<int, int> PII;
    const int INF = 0x3f3f3f3f;
    const int N = 1010;
    struct sa
    {
    long double v, w;
    };
    long double w[N], v[N];

    long double n, m;
    signed main()
    {
    IOS;
    vector<sa> g;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
    cin >> v[i];
    for (int i = 1; i <= n; ++i)
    {
    long double x;
    cin >> x;
    w[i] = x / v[i];
    g.push_back(sa{v[i], w[i]});
    }

    sort(g.begin(), g.end(), [&](sa a, sa b) { return a.w > b.w; });
    long double ans = 0;
    for (auto [v, w] : g)
    {
    if (v <= m)
    {
    ans += v * w;
    m -= v;
    }
    else
    {
    ans += m * w;
    break;
    }
    }
    printf("%.2Lf", ans);

    return 0;
    }
使用搜索:谷歌必应百度