UVA 12325 - Zombie's Treasure Chest
2015-06-01 20:47
357 查看
紫书P210 例题7-11。
此题分3种情况讨论:
1、S1很小,S2很大时, 直接枚举宝物2的个数0~N/S2。
2、S1很大,S2很小时, 直接枚举宝物1的个数0~N/S1。
3、S1很小,S2很小时,当两种宝物体积相同时,比较S2*V1与S1*V2的大小,如果前者大,那应该优先拿前者。由此可得后者最多拿S1-1个。反之,前者最多拿S2-1个。
此题分3种情况讨论:
1、S1很小,S2很大时, 直接枚举宝物2的个数0~N/S2。
2、S1很大,S2很小时, 直接枚举宝物1的个数0~N/S1。
3、S1很小,S2很小时,当两种宝物体积相同时,比较S2*V1与S1*V2的大小,如果前者大,那应该优先拿前者。由此可得后者最多拿S1-1个。反之,前者最多拿S2-1个。
#include <stdio.h> #include <algorithm> #include <math.h> #include <string.h> using namespace std; int main() { long long n, s1, v1, s2, v2; int T; scanf("%d", &T); for(int _ = 1; _ <= T; _++) { long long ans = 0; scanf("%lld %lld %lld %lld %lld", &n, &s1, &v1, &s2, &v2); if(n / s1 <= 50000) { int k = n / s1; for(int i = 0; i <= k; i++) { ans = max(ans, i * v1 + (n - i * s1) / s2 * v2); } } else if(n / s2 <= 50000) { int k = n / s2; for(int i = 0; i <= k; i++) { ans = max(ans, i * v2 + (n - i * s2) / s1 * v1); } } else { if(s2*v1 > s1*v2) { for(int i = 0; i < s1; i++) { ans = max(ans, i * v2 + (n - i * s2) / s1 * v1); } } else { for(int i = 0; i < s2; i++) { ans = max(ans, i * v1 + (n - i * s1) / s2 * v2); } } } printf("Case #%d: %lld\n", _, ans); } }
相关文章推荐
- 《算法竞赛入门经典》解题索引
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes