hdu 4091 Zombie’s Treasure Chest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2461    Accepted Submission(s): 494


Problem Description
  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,  有两种宝石, 数量无限, 不能切割。  分别为 size1   value 1 size2 value2

问背包能装最大的价值?


思路 : 线性规划问题。  有一组坑爹数据  100 3 3 7 7   一般的会输出 99     但是结果是 100  各10颗


线性规划知识, x, y 分别为 取两种宝石的数量   目标函数 : z = v1 * x + v 2 * y;      K = -(v1 / v2);

1.   x >= 0;

2.   y >= 0;

3.   s1 * x + s2 + y <= N;   k = - (s1 / s2);




若  abs(K)  > abs (k)  那么 将相交与B。   若abs(K) ==  abs(k)  整条直线都可以。 若 abs(K) < abs(k) 相交与A

其实   abs(K) = v1 / v2  >  abs(k) = s1 / s2   正好是 价值比的比较,   v1 / s1 > v2 / s2    ,v1价值大, 就应该  x  大些, 取1宝石多些。

同理  价值相同  就应该 x, y 取什么都可以,  v2 价值大, 就应该 y 大些,  取2宝石多些。


但是  宝石不能切割, 所以。 就形成了一个区域, 在最优解附近。 所以区域附近的点 需要枚举。

不过有一个不变的是, 在两宝石的体积的最小公倍数内, 肯定取价值大。

而在最小公倍外的,就要分别枚举。 不能盲目的取价值大。 

 比如一个例子,   20 4 5 6 8   答案是 26  = 2 * 5 + 2 * 8。  若只取价值大的, 会装不满。 没取到最优解。    

4 与 6 的最小公倍数是 12.  那每一个12的体积  就应该取  2个  宝石2   因为宝石2价值大。

剩余的8 就有取枚举, 0 * 5 + 1 * 6 , 或者  1 * 5 + 0 * 6,  或者 2 * 5 + 0 * 6   那么就取2个宝石1.  就能装满了, 并且价值最大。

但是 如果是 15 4 5 6 8 的话,  那么按照这方法就会输出   16   只能取到  2个 宝石2  剩余3体积, 不能取到任意宝石。 

答案应该是 18 = 2 * 5 + 1 * 8,   少取一个宝石2,腾出6体积,并 利用剩余的3体积的2体积 取两颗宝石1,价值更大。

所以,面对这问题。  我们就应该至少腾出一个公倍数的空间才枚举。  不然就会出错。

#include <stdio.h>
__int64 f(__int64 a , __int64 b) {
    __int64 tempa = a, tempb = b, r;
    if(a < b) {
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
    }
    do {
        r = a % b;
        a = b;
        b = r;    
    }while(r);
    return tempa / a * tempb;
}
int main() {
    int t, i, count = 1;
    __int64 sum, temp, p, n, s[2], v[2], max, minv, maxv, gbs, j, k;
    scanf("%d", &t);
    while(t--) {
        scanf("%I64d%I64d%I64d%I64d%I64d", &n, &s[0], &v[0], &s[1], &v[1]);
        gbs = f(s[0], s[1]);
        p = v[0] * s[1] - v[1] * s[0];
        printf("Case #%d: ", count++);
        if(p > 0)
            i = 0;
        else
            i = 1;
        minv = s[0] > s[1] ? 1 : 0;
        maxv = (minv == 0) ? 1 : 0;
        temp = n / gbs;
        n = n % gbs;
        if(temp) {
            temp--;
            n += gbs;
        }
        sum = temp * (gbs / s[i]) * v[i];
        k = n / s[maxv];
        max = 0;
        for(j = 0; j <= k; ++j) {
            temp = j * v[maxv] + (n - j * s[maxv]) / s[minv] * v[minv];
            if(temp > max)
                max = temp;
        }
        printf("%I64d\n", sum + max);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值