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): 1708    Accepted Submission(s): 357


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 可定超时所以可以减减枝,由于LCM(s1,s2)*k==x*s1+y*s2总存在这样的解;
故只需枚举s%LCM(s1,s2)即可;但是要注意了考虑是否存在亏损可以枚举s%LCM(s1,s2)+LCM(s1,s2);
为什么只考虑只加一个LCM(s1,s2);一个LCM(s1,s2)=x*s1+y*s2可以完全覆盖使用两个重复第一个而已;
View Code
 1 #include<iostream>
 2 #include<fstream>
 3 #include<cstdio>
 4 using namespace std;
 5 
 6 long long GCD(long long a,long long b)
 7 {
 8     long long t;
 9     if(a<b) {t=a;a=b;b=t;}
10     if(a%b==0) return b;
11     else return GCD(b,a%b);
12 }
13 
14 long long LCM(long long a,long long b)
15 {
16     return a/GCD(a,b)*b;
17 }
18 
19 int main()
20 {
21 //    freopen("in.txt","r",stdin);
22 //    freopen("out.txt","w",stdout);
23     long long s,s1,v1,s2,v2,ans,n,m,i,t,max,L;
24     int test,k=1;
25     double k1,k2;
26     scanf("%d",&test);
27     while(test--)
28     {
29         scanf("%I64d%I64d%I64d%I64d%I64d",&s,&s1,&v1,&s2,&v2);
30         L=LCM(s1,s2);
31         n=s/L;m=s%L;
32         if(n){n--;m+=L;}
33         n=n*L;
34         k1=double(v1)/s1;
35         k2=double(v2)/s2;
36         if(k1>=k2)ans=n/s1*v1;
37         else ans=n/s2*v2;
38         if(s1<s2)
39         {
40             t=s1;s1=s2;s2=t;
41             t=v1;v1=v2;v2=t;
42         }
43         max=0;
44         for(i=0;i*s1<=m;i++)
45         {
46             t=i*v1+(m-i*s1)/s2*v2;
47             if(max<t) max=t;
48         }
49         printf("Case #%d: %I64d\n",k++,max+ans);
50     }
51     return 0;
52 }
53 /*
54 2
55 10 3 11 2 6
56 */

方法二: x*s1+y*s2<=s   z=x*v1+y*v2  看到这两个式子我们很容易想到线性规划
所以第二种方法采用的是线性规划。

令L1:x*s1+y*s2=s  L2:x*v1+y*v2=z;

k1、k2分别是L1 和L2的斜率;设(x1,y1)为L1 和L2 的交点;

if(k1>k2) x2=s/s1

          故最优解在[x1,x2]之间

if(k1<k2)同上;

对于k1==k2 单独考虑;

这样做是可以的但是要考虑优化不然会超时的,假如L1和L2的斜率很接近就很费时;

转载于:https://www.cnblogs.com/zhourongqing/archive/2012/05/11/2496176.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值