HDU 4091Zombie’s Treasure Chest (数学加暴力)

来源:互联网 发布:淘宝小店如何推广 编辑:程序博客网 时间:2024/05/21 14:06

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



题意:已知s s1 v1 s2 v2; 设有 x 个 s1 和 y 个 s2 ;满足 x*s1+y*s2<=s 求 max(x*v1+y*v2)

如果直接枚举 0 至 s/s1 可定超时所以可以减减枝,分两个部分1. LCM(s1,s2)的倍数 对于两个物品直接用价值比高的来算;
2.需枚举s%LCM(s1,s2)即可;但是要注意了考虑是否存在亏损可以枚举s%LCM(s1,s2)+LCM(s1,s2);
据说应该至少腾出一个公倍数的空间才暴力枚举。。。

其实 加两个lcm,影响不大。


附:

为什么求最小公倍数呢?
                   a = lcm /s1 , b = lcm/s2 。 最小公倍数除以s1 s2 后的到 a,b  。 a 个宝石一 和 b 个宝石二 所占的空间是相同的。
                  在同样的空间里,就要比较谁的总价值更大 即max(a*v1,b*v2);


#include<cstdio>#define  ll __int64#include<algorithm>using namespace std;int gcd(ll a,ll b){    if(b==0)        return a;    return gcd(b,a%b);}int main(){    ll n,s1,v1,s2,v2;    int T;    scanf("%d",&T);    for(int t=0; t<T; t++ )    {        scanf("%I64d%I64d%I64d%I64d%I64d",&n,&s1,&v1,&s2,&v2);        ll ans=0;        ll extn=s1/gcd(s1,s2)*s2;;       // prllf("%d \n",extn);        if(n>extn)         //第一部分,lcm的倍数        {            ll x=(n-extn)/extn;            ans+=x*max((extn/s1)*v1,(extn/s2)*v2);            n-=x*extn;        }        ll k,ks1,kv1,kv2,ks2;        if(s1>s2)           {               k=n/s1;               ks1=s1,kv1=v1,ks2=s2,kv2=v2;           }        else           {               k=n/s2;                ks1=s2,kv1=v2,ks2=s1,kv2=v1;           }        ll maxn=0;        for(ll i=0; i<=k; i++)    //第二部分暴力穷举        {            ll j=(n-i*ks1)/ks2;            maxn=max(i*kv1+j*kv2,maxn);           // prllf("%d %d %d\n",i,j,maxn);        }        ans+=maxn;        printf("Case #%d: %I64d\n",t+1,ans);    }    return 0;}



0 0
原创粉丝点击