G - Zombie’s Treasure Chest(动态规划专项)

G - Zombie’s Treasure Chest
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status

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 和两种宝石的 size(体积) 和 value(价值) ,即 N s1 v1 s2 v2 ,已知这五个两,求利用这个宝箱,战士能够带走多大价值的宝石。 五个数的范围是 int 。
思路:
算法思路:1.假设变量N,S1,V1,S2,V2
                         2.求S1,S2的最小公倍数LCM
                         3.求商s=N/LCM,余数y=N%LCM
                         4.s>=1 则s--,y+=LCM  
                         5.然后就是枚举,从nsize枚举到0,注意这之前有个很重要的技巧避免超时:枚举s1,s2中拿的量少的
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<queue>
 6 
 7 using namespace std;
 8 
 9 long long gcd(long long a,long long b)
10 {
11     if(b==0) return a;
12     return gcd(b,a%b);
13 }
14 
15 int main()
16 {
17     freopen("1.txt","r",stdin);
18     int cas,t;
19     long long n,s1,v1,s2,v2;
20     long long g,l,res,rv1,rv2,i,mx,tmp,j;
21     cin>>t;
22     for(cas=1;cas<=t;cas++)
23     {
24         cin>>n>>s1>>v1>>s2>>v2;
25         if(s1>s2)
26         {
27             swap(s1,s2);
28             swap(v1,v2);
29         }
30         g=gcd(s1,s2);
31         l=s1/g*s2;
32         rv1=l/s1*v1;
33         rv2=l/s2*v2;
34         if(n>=l+l)
35         {
36             res=(n-l)/l*(rv1>rv2?rv1:rv2);
37             n=n%l+l;
38         }
39         else
40         {
41             res=0;
42         }
43         mx=0;
44         for(i=0,j=0;i<=n;i+=s2,j++)
45         {
46             tmp=j*v2+(n-i)/s1*v1;
47             if(tmp>mx)
48             {
49                 mx=tmp;
50             }
51         }
52         res+=mx;
53         cout<<"Case #"<<cas<<": "<<res<<endl;
54     }
55     return 0;
56 }
View Code

 

转载于:https://www.cnblogs.com/zhangchengbing/p/3355229.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值