跳转至

搭配出售#
发布于2021-04-16
上次编辑2021-04-16

问题描述#

服装店新进了 a 条领带,b 条裤子,c 个帽子,d 件衬衫,现在要把这些搭配起来售卖。有三种搭配方式,一条领带和一件衬衫,一条裤子和一件衬衫,一个帽子和一件衬衫。卖出一套领带加衬衫可以得到 e 元,卖出一套裤子加衬衫可以得到 f 元,卖出一套帽子加衬衫可以得到 g 元。现在你需要输出最大获利。

格式:

输入:
- 一行 7 个整数分别表示 a, b, c, d, e, f, g 。
输出:
- 最大获利。

示例:

输入:2 3 4 5 6 7 8
输出:39
解释:4 个帽子加 4 件衬衫获利 32 元,1 条裤子加 1 件衬衫获利 7 元,一共得到 39 元。

解题思路#

 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
import sys
from typing import List

readline = sys.stdin.readline


def readint() -> int:
    return int(readline().strip())


def readstr() -> str:
    return readline().strip()


def readints() -> List[int]:
    return list(map(int, readline().strip().split()))


a, b, c, d, e, f, g = readints()
A = [(e, a), (f, b), (g, c)]
A.sort(reverse=True)
ans = 0
for p, t in A:
    k = min(d, t)
    d -= k
    ans += p * k
print(ans)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    int a, b, c, d, e, f, g;
    cin >> a >> b >> c >> d >> e >> f >> g;
    vector<pair<int, int>> A;
    A.emplace_back(e, a);
    A.emplace_back(f, b);
    A.emplace_back(g, c);
    sort(A.begin(), A.end(), greater<>());
    long long ans = 0;
    for (auto &[p, t] : A) {
        int k = min(d, t);
        d -= k;
        ans += 1LL * p * k;
    }
    cout << ans << '\n';
    return 0;
}
返回顶部

在手机上阅读