【题目链接】 点击打开链接
【题意】你有一个体积为N的箱子和两种数量无限的宝物。宝物1的体积为s1,价值为v1,宝物2的体积为s2,价值为v2.输入均为32位带符号整数。你的任务是计算最多能装多大价值的宝物。例如n=100,s1=v1=34,s2=5,v2=3,那么答案就为86,方法是装2个宝物1,装6个宝物2,。每种宝物都必须是拿非负整数个。
【解题方法】紫书的解题思路。

最容易想到的是枚举法。枚举宝物1的个数,然后多拿宝物2.这样做的时间复杂度是O(n/s1),当n和s1相差非常悬殊时,效率非常低下。同样,枚举宝物2的个数,道理是一样的!

所以这个方法不奏效的条件是:s1和s2都很小,而n很大。

幸运的是->s1和s2都很小的时候,有另外一种枚举法B:s2个宝物1和s1个宝物2的体积是一样大的,而价值分别为s2*v1,和s1*v2,

1.如果前者比较大,那么证明宝物1的性价比要比宝物2大。如果n=s1*s2的话,在宝物数量的选择上,宝物2最多只会拿s1-1个(否则的话,我完全可以将s1个宝物2换成价值更高的s2个宝物1);

2.如果后者比较大的话,那么证明宝物2的性价比要比宝物1大。如果n=s1*s2的话,在宝物数量的选择上,宝物1最多只会拿s2-1个(否则的话,我完全可以将s2个宝物1换成价值更高的s1个宝物2);

不管是哪种情况,枚举量都只有s1或者s2.

这样就得到了一个比较“另类”的分类枚举法。

当n/s1比较小的时候枚举宝物1的个数,否则,枚举宝物2的个数,再否则就说明s1和s2都比较小,执行枚举法B。


【AC代码】

//
//Created by BLUEBUFF 2016/1/8
//Copyright (c) 2016 BLUEBUFF.All Rights Reserved
//

#pragma comment(linker,"/STACK:102400000,102400000")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstdlib>
#include <cstring>
#include <sstream> //isstringstream
#include <iostream>
#include <algorithm>
using namespace std;
//using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define REP1(i, a, b) for(int i = a; i < b; i++)
#define REP2(i, a, b) for(int i = a; i <= b; i++)
#define REP3(i, a, b) for(int i = a; i >= b; i--)
#define CLR(a, b)     memset(a, b, sizeof(a))
#define MP(x, y)      make_pair(x,y)
const int maxn = 12;
const int maxm = 2e5;
const int maxs = 10;
const int maxp = 1e3 + 10;
const int INF  = 1e9;
const int UNF  = -1e9;
const int mod  = 1e9 + 7;
//int gcd(int x, int y) {return y == 0 ? x : gcd(y, x % y);}
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//head
LL v, s1, v1, s2, v2;
int main()
{
    int T, ks = 0;
    scanf("%d", &T);
    while(T--){
        LL ans = 0;
        scanf("%lld%lld%lld%lld%lld", &v, &s1, &v1, &s2, &v2);
        if(v1 * s2 >= v2 * s1){
            if(v / s1 > s1){
                for(int i = 0; i < s1; i++) ans = max(ans, i * v2 + (v - i * s2) / s1 * v1);
            }
            else{
                for(int i = 0; i <= v / s1; i++) ans = max(ans, i * v1 + (v - i * s1) / s2 * v2);
            }
        }
        else{
            if(v / s2 > s2){
                for(int i = 0; i < s2; i++) ans = max(ans, i * v1 + (v - i * s1) / s2 * v2);
            }
            else{
                for(int i = 0; i <= v / s2; i++) ans = max(ans, i * v2 + (v - i * s2) / s1 * v1);
            }
        }
        printf("Case #%d: %lld\n", ++ks, ans);
    }
    return 0;
}