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-[分类枚举]

时间: 2024-08-02 02:53:36

UVa 12325 - Zombie's Treasure Chest-[分类枚举]的相关文章

UVa 12325 Zombie&#39;s Treasure Chest【暴力】

题意:和上次的cf的ZeptoLab的C一样,是紫书的例题7-11 不过在uva上交的时候,用%I64d交的话是wa,直接cout就好了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include&l

12325 - Zombie&#39;s Treasure Chest.

简单枚举+巧妙躲避大枚举量 #include<bits/stdc++.h> using namespace std; long long n,s1,v1,s2,v2,total; int main() { ios::sync_with_stdio(false); long long T,maxn=0; cin>>T; while(T--) { cin>>n>>s1>>v1>>s2>>v2; total=0; long lo

hdu 4091 Zombie’s Treasure Chest 贪心+枚举

转自:http://blog.csdn.net/a601025382s/article/details/12308193 题意: 输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝石数目无限,问背包里能放下的最大价值? 题解: 看过去很像完全背包,可数据很大(虽然没给出,也能猜到,不然太水了),所以不能用背包求.又只有两种物品,想到了贪心,将价值与体积比大(称为价值比)的优先放入.但体积限制,这样还不可以,还需要枚举减少价值比大的宝石个数,是否可以增大所求价值.又我们可以知道

【例题 7-11 UVA - 12325】Zombie&#39;s Treasure Chest

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 1.N/s1 < 1e6 枚举1的个数 2.N/s2<1e6 枚举2的个数 3.s1和s2的值较小 假设买了s2个1和s1个2 那么这两种物品占的体积就一样大了. 即都为s1s2 而第一种物品价值为s2v1第二种物品价值为s1v2 那么 如果s2v1>s1v2的话. 可以想见,如果第二种物品的数量超过了s1的话,显然可以把它占的体积都用来买物品1,因为那样更优. 则我们第二种物品最多只要枚举到s1就可以了. 同理s2v1

UVA 12325 Zombie&#39;sTreasureChest

看上去非常像背包的问题,但是体积太大了. 线性规划的知识,枚举附近点就行了,优先选性价比高的, 宝物有两种体积为S0,价值V0,体积S1,价值V1. 枚举分以下几种: 1:枚举拿宝物1的数量,然后尽量多拿宝物2:O(N/S0) 2:枚举拿宝物2的数量,同上:O(N/S1) 3.贪心,尽量选性价比高的 令gcd(S0,S1)= t,S1/t*S0 = S0/t*S1:体积相同的情况下尽量选价值高的,如果S1*V0>S0*V1大,那么枚举拿宝物2的数量,最多S0/t-1个否则一定可以换成S1/t个宝

一道看似dp实则暴力的题 Zombie&#39;s Treasure Chest

 Zombie's Treasure Chest 本题题意:有一个给定容量的大箱子,此箱子只能装蓝宝石和绿宝石,假设蓝绿宝石的数量无限,给定蓝绿宝石的大小和价值,要求是获得最大的价值 题解:本题看似是dp中的背包问题,但是由于数据量太大,用dp肯定会超时,所以只能寻找另外一种思路,可以用贪心加暴力,先求出两种宝石大小的最小公倍数com,然后将N/com-com,与N%comkanchengs看成是两个部分(想想应该明白).将前一个部分,放入单位价值量最高的那个,对于后面那个部分直接将S1的数量从

hdu4091 Zombie’s Treasure Chest

Zombie’s Treasure Chest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4442    Accepted Submission(s): 889 Problem Description Some brave warriors come to a lost village. They are very lucky an

UVa12325, Zombie&#39;s Treasure Chest

反正书上讲的把我搞得晕头转向的,本来就困,越敲越晕...... 转网上一个大神写的吧,他分析的很好(个人感觉比书上的清楚多了) 转:http://blog.csdn.net/u010536683/article/details/12450865 UVa12325, Zombie's Treasure Chest

uva 12325 枚举暴力 b

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 br