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 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

卡题在即使v1/s1>v2/s2且s2>s1

#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
long long  n,s1,v1,s2,v2;
const double eps=1e-11;
long long gcd(long long a,long long b){
    return b==0?a:gcd(b,a%b);
}
long long lcm(long long a,long long b){
if(a*b==0)return 0;
    return a*b/(gcd(a,b));
}
int main(){
    int T;
    scanf("%d",&T);
    for(int ca=1;ca<=T;ca++){
            scanf("%I64d%I64d%I64d%I64d%I64d",&n,&s1,&v1,&s2,&v2);
if(s1>s2){
    swap(s1,s2);swap(v1,v2);
}
        long long LCM=lcm(s1,s2);
        long long t1=n>LCM?(n-LCM)/LCM:0;
        n-=t1*LCM;
        long long ans=(n/s1)*v1+((n%s1)/s2)*v2;
        long long a=n/s2;
        for(long long i=0;i<=a;i++){
            long long tmp=(i*v2)+((n-i*s2)/s1)*v1;
            ans=max(ans,tmp);
        }
        ans+=t1*max((LCM/s1)*v1,(LCM/s2)*v2);
        printf("Case #%d: %I64d\n",ca,ans);
    }
    return 0;
}

  

时间: 2024-10-08 14:44:36

hdu4091 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

UVa 12325 - Zombie&#39;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 brin

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

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

UVa12325, Zombie&#39;s Treasure Chest

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

【例题 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

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

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

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱*

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱 题意: 给个序列,A与B轮流取数,谁取的数总和大谁赢.每次只能取序列两端,问A能取的数总和最大是多少.假设两人都用最优策略.序列大小≤5000 题解: dp.f[i][j][0]=max(f[i+1][j][1]+a[i],f[i][j-1][1]+a[j]),f[i][j][1]=min(f[i+1][j][0],f[i][j-1][0]). 代码: 1 #include <cstdio> 2 #includ

BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )

dp( l , r ) = sum( l , r ) - min( dp( l + 1 , r ) , dp( l , r - 1 ) ) 被卡空间....我们可以发现 l > r 是无意义的 , 所以可以省下一半的空间 -------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<a