题意:
有A,B,C三种披萨,A,B是单独种类的披萨,C是A和B的双拼披萨,他们的价位各是a, b, c,现在你需要 x 个A披萨,y 个B披萨,你可以单独买,或者买多个双拼披萨自己拆开重组,求最小花费。
思路:
贪心题,你一共就三种买法,
第一种,分别买 x 个A披萨,以及 y 个B披萨;
第二种,比较出x 和 y 中较小者,买 min(x, y) * 2 个双拼,这样少的那种披萨就已经凑齐了,再买 一共需要的多的那种披萨个数,减去双拼组成的多的那种披萨个数 即可。
第三种,买个数多的那种的双拼乘2,即 max(x, y) * 2 个双拼,这样少的也就自然凑齐了,可能会造成浪费,但是也是一种买法。
求以上三种花费的最小值即可。
本人AC代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int a, b, c, x, y;
int ans;
int main() {
scanf("%d %d %d %d %d", &a, &b, &c, &x, &y);
int sum = a * x + b * y;
int s = min(x, y) * 2 * c;
if(x >= y) s += (x - y) * a;
else s += (y - x) * b;
int s0 = max(x, y) * 2 * c;
s = min(s, s0);
ans = min(sum, s);
printf("%d\n", ans);
}