https://vjudge.net/problem/UVA-12325
题意:有一个体积为N的箱子和两种数量无限的宝物。宝物1的体积为S1,价值为V1‘宝物2的体积为S2,价值为V2。计算出最多能装多大价值的宝物。
思路:题目很清楚就是暴力枚举,但是如果不简化枚举的话肯定是会超时的,如果N/S1比较小,那就枚举宝物1的个数,如果N/S2比较小,则枚举宝物2的个数。还有一种情况就是S1和S2都很小,S2个宝物1和S1个宝物2的体积相等,而价值分别为S2*V1和S1*V2。如果前者比较大,则宝物2最多只会拿S1-1个(否则则可以把S1个宝物2换成S2个宝物1);如果后者比较大,则宝物1最多只会拿S2-1个。
1 #include<iostream> 2 using namespace std; 3 4 long long N, S1,V1, S2,V2; 5 long long maxn ; 6 7 void solve1() 8 { 9 for (int i = 0; i <= N / S1; i++) 10 { 11 long long number = (N - i*S1) / S2; 12 long long sum = i*V1 + number*V2; 13 if (sum > maxn) maxn=sum; 14 } 15 } 16 17 void solve2() 18 { 19 for (int i = 0; i <= N / S2; i++) 20 { 21 long long number = (N - i*S2) / S1; 22 long long sum = i*V2 + number*V1; 23 if (sum > maxn) maxn=sum; 24 } 25 } 26 27 void solve3() 28 { 29 if (S2*V1<S1*V2) 30 { 31 for (int i = 0; i < S2 && i<=N/S1; i++) 32 { 33 long long number = (N - i*S1) / S2; 34 long long sum = i*V1 + number*V2; 35 if (sum > maxn) maxn = sum; 36 } 37 } 38 else 39 { 40 for (int i=0; i < S1&&i<=N/S2; i++) 41 { 42 long long number = (N - i*S2) / S1; 43 long long sum = i*V2 + number*V1; 44 if (sum > maxn) maxn = sum; 45 } 46 } 47 } 48 49 int main() 50 { 51 int n, kase = 0; 52 cin >> n; 53 while (n--) 54 { 55 cin >> N >> S1 >> V1 >> S2 >> V2; 56 maxn = 0; 57 if (S1<100 && S2<100) 58 solve3(); 59 else if (N / S1 < N / S2) solve1(); 60 else solve2(); 61 cout << "Case #" << ++kase << ": " << maxn << endl; 62 } 63 return 0; 64 }