UVa 12325 - Zombie's Treasure Chest-[分类枚举]

Posted kiraa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa 12325 - Zombie's Treasure Chest-[分类枚举]相关的知识,希望对你有一定的参考价值。

12325 Zombie’s Treasure

  Chest 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

解题思路:

  一般的思想是枚举宝物1(假设宝物s1>s2,当s2>s1时相反)的数量n/s1,然后尽可能多拿宝物2,得到最大价值。这种方法的时间复杂度是

o(n/Smax)。但是当n/Smax很大时这种方法就行不通了。幸运的当n>>s1,s2时可以比较他们的性价比(价值v/大小s):

  当v1/s1<v2/s2时,说明宝物2的性价比高,那么宝物1最多只能拿s2-1个,因为,假如宝物1的数量>=s2,那么可以将s2个宝物1的换成s1个宝物2,体积不变,价值增加,这种情况下枚举量就是s2-1。时间复杂度为o(Smax-1);

如何定义远大于呢?可以认为n/Smax >10^5时就是远大于了,因为此时最大枚举量不过10^5,不影响效率。

代码如下:

 1 //
 2 //  main.cpp
 3 //  Zombie‘s Treasure Chest
 4 //
 5 //  Created by 胡佳成 on 16/3/25.
 6 //  Copyright © 2016年 胡佳成. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 #include <cstdio>
11 #include <ctime>
12 #include <algorithm>
13 #define print_time_ printf("time : %f\n",double(clock())/CLOCKS_PER_SEC)
14 using namespace std;
15 int N,S1,V1,S2,V2;
16 const int limit=50000;
17 typedef long long LL;
18  LL most_value(int num_s2,int s1,int v1,int s2,int v2){//默认枚举s2
19     LL val=0;
20     for(LL i=0;i<=num_s2;i++)
21         val=max(val,(N-i*s2)/s1*v1+i*v2);
22             
23     return val;
24 }
25 int main() {
26     int T;
27     scanf("%d",&T);
28     for(int i=1;i<=T;i++){
29         scanf("%d%d%d%d%d",&N,&S1,&V1,&S2,&V2);
30         LL value=0;
31         if(S1>S2){
32             swap(S1, S2);
33             swap(V1, V2);
34         }
35         if(N/S2<=limit){
36             value=most_value(N/S2, S1, V1, S2, V2);
37         }
38         else if(LL(S2)*V1<LL(S1)*V2){
39             value=most_value(S2-1, S2, V2, S1, V1);
40         }
41         else{
42             value=most_value(S1-1, S1, V1, S2, V2);
43         }
44         printf("Case #%d: %lld\n",i,value);
45     }
46     
47     //print_time_;
48     return 0;
49 }

 

  

  

 

以上是关于UVa 12325 - Zombie's Treasure Chest-[分类枚举]的主要内容,如果未能解决你的问题,请参考以下文章

例题 7-11 UVA - 12325Zombie's Treasure Chest

UVa 12325 宝箱

uva 12325 宝箱

uva 12325 枚举暴力 b

UVA12325-注意提高效率的思路

(暴力)UVA - 12325 Editing a Book