Zombie‘s Treasure Chest(枚举)

作者:TXY

题目描述:
Some brave warriors come to a lost village. They are very lucky and find a lot of treasures and a big treasure chest, but with angry zombies.

The warriors are so brave that they decide to defeat the zombies and then bring all the treasures back. A brutal long-drawn-out battle lasts from morning to night and the warriors find the zombies are undead and invincible.  

Of course, the treasures should not be left here. Unfortunately, the warriors cannot carry all the treasures by the treasure chest due to the limitation of the capacity of the chest. Indeed, there are only two types of treasures: emerald and sapphire. All of the emeralds are equal in size and value, and with infinite quantities. So are sapphires.

Being the priest of the warriors with the magic artifact: computer, and given the size of the chest, the value and size of each types of gem, you should compute the maximum value of treasures our warriors could bring back.

Input

There are multiple test cases. The number of test cases T (T ≤ 200) is given in the first line of the input file. For each test case, there is only one line containing five integers N, S1, V1, S2, V2, denoting the size of the treasure chest is N and the size and value of an emerald is S1 and V1, size and value of a sapphire is S2, V2. All integers are positive and fit in 32-bit signed integers.

Output

For each test case, output a single line containing the case number and the maximum total value of all items that the warriors can carry with the chest.

Sample Input

2

100 1 1 2 2

100 34 34 5 3

Sample Output 

Case #1: 100

Case #2: 86

题目大意: 

你有一个体积为N的箱子和两种数量无限的宝物。宝物1的体积为S1,价值为V1;宝物2的体积为S2,价值为V2。输入均为32位带符号整数。你的任务是计算最多能装多大价值的宝物。

解题思路:

    分类枚举

    枚举法A:判断S1与S2哪个更大,枚举体积更大宝物的个数,然后尽量多的拿另一种宝物。此时时间复杂度为O(N/S(大者))。此时,当S1和S2都很小,N很大,那么运行效率极低。

    当S1和S2都很小,N很大时。用枚举法B:S2个宝物1和S1个宝物2体积相等,而价值分别为S2*V1和S1*V2。如果前者比较大,说明宝物1的性价比更高V1/S1>V2/S2,那么就要尽可能多的拿宝物1来保证拿的总价值更高,而此时,宝物2最多只会拿S1-1个(否则可以把S1个宝物2换成S2个宝物1);如果后者比较大,则宝物1最多只会拿S2-1个。不管哪种方法,枚举量只有S1或者S2。

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
int main() {
    int t,i,kase = 0;
    long long n,s1,s2,v1,v2;
    long long Ans = 0;
    scanf("%d",&t);
    while(t--) {
        scanf("%lld",&n);
        scanf("%lld %lld %lld %lld",&s1,&v1,&s2,&v2);
        Ans = 0;
        if(s1 > s2) {
        	//使S2都大于S1,便于使用枚举法A 
            swap(s1,s2);
            swap(v1,v2);
        }
		if(n / s2 > 100000) {
			//S1和S2都很小,N很大时 ,枚举法B 
            for(i=0; i<s1; i++)
                Ans = max(Ans,v2 * i + (n - s2 * i ) / s1 * v1);
            for(i=0; i<s2; i++)
                Ans = max(Ans ,v1 * i + (n - s1 * i) / s2 * v2);
        }
        else {
            for(i=0; i<=n/s2; i++)
            //枚举法A 
                Ans = max(Ans,i * v2 + (n - i * s2) / s1 * v1);
        }
        printf("Case #%d: %lld\n",++kase,Ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值