C - Half and Half
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.
Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.
Constraints
- 1≤A,B,C≤5000
- 1≤X,Y≤105
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B C X Y
Output
Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
using namespace std;
int pra, prb, prc;
int x, y;
LL ans = 0;
int main()
{
while (cin >> pra >> prb >> prc >> x >> y) {
ans = 0;
if (prc * 2 < pra + prb) {
ans += min(x, y)*prc * 2;
if (x > y)
ans += (x - y)*pra;
else ans += (y - x)*prb;
} else {
ans = x*pra + y*prb;
}
if (prc * 2 * max(x, y) < ans)
ans = prc * 2 * max(x, y);
cout << ans << endl;
}
return 0;
}
贪心算法,如果存在C的价格乘2低于A+B,对于A和B都要买的情况,全部选择C来进行购买。之后再分类讨论就OK了。