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个。
#include<iostream>
using namespace std; long long N, S1,V1, S2,V2;
long long maxn ; void solve1()
{
for (int i = ; i <= N / S1; i++)
{
long long number = (N - i*S1) / S2;
long long sum = i*V1 + number*V2;
if (sum > maxn) maxn=sum;
}
} void solve2()
{
for (int i = ; i <= N / S2; i++)
{
long long number = (N - i*S2) / S1;
long long sum = i*V2 + number*V1;
if (sum > maxn) maxn=sum;
}
} void solve3()
{
if (S2*V1<S1*V2)
{
for (int i = ; i < S2 && i<=N/S1; i++)
{
long long number = (N - i*S1) / S2;
long long sum = i*V1 + number*V2;
if (sum > maxn) maxn = sum;
}
}
else
{
for (int i=; i < S1&&i<=N/S2; i++)
{
long long number = (N - i*S2) / S1;
long long sum = i*V2 + number*V1;
if (sum > maxn) maxn = sum;
}
}
} int main()
{
int n, kase = ;
cin >> n;
while (n--)
{
cin >> N >> S1 >> V1 >> S2 >> V2;
maxn = ;
if (S1< && S2<)
solve3();
else if (N / S1 < N / S2) solve1();
else solve2();
cout << "Case #" << ++kase << ": " << maxn << endl;
}
return ;
}