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.
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可以完全覆蓋使用兩個重復第一個而已;

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的斜率很接近就很費時;