12325 Zombie ' s treasure
Chest Some Brave warriors come to a lost village. They is very lucky and find a lot of treasures and a big treasure chest, but with angry zombies. The Warriors is so brave, they decide to defeat, the zombies and then bring all of the treasures back. A brutal long-drawn-out battle lasts from morning to night and the Warriors find the zombies is undead and invincible. Of course, the treasures should not being left here. Unfortunately, the Warriors cannot carry all the treasures by the treasure chest to the due of the limitation of The chest. Indeed, there is only a types of Treasures:emerald and sapphire. All of the emeralds is equal in size and value, and with infinite quantities. So is 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 gems, you should compute the maximum value of treasures we warriors could bring back.
Input
There is multiple test cases. The number of test cases T (t≤200) is given on the first line of the input file. For each test case, there was only one line containing five integers N, S1, V1, S2, V2, denoting the size of the treasure C Hest is N and the size and value of an emerald are S1 and V1, size and value of a sapphire is S2, V2. All integers is 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 any items that the Warr Iors 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
Problem Solving Ideas:
The general idea is to enumerate the treasures of the 1 (assuming that the treasure s1>s2, when s2>s1 the opposite) of the number n/s1, and then take as many treasures as 2, to get maximum value. The time complexity of this method is
O (N/smax). But this approach doesn't work when the N/smax is big. Fortunately, when N>>S1,S2 can compare their price/performance ratio (value v/size s):
When the V1/S1<V2/S2, the description of the Treasure 2 cost-effective, then the Treasure 1 can only take s2-1, because, if the treasure 1 of the number >=S2, then you can s2 a treasure 1 for S1 a Treasure 2, volume unchanged, value increases, in this case the enumeration is s2-1. Time Complexity of O (Smax-1);
How do you define much more than that? It can be thought that N/smax >10^5 is much larger than that, because at this time the maximum enumeration is 10^5, which does not affect efficiency.
The code is as follows:
1 //2 //main.cpp3 //Zombie ' s treasure Chest4 //5 //Created by Hu Jiacheng on 16/3/25.6 //copyright©2016 year Hu Jiacheng. All rights reserved.7 //8 9#include <iostream>Ten#include <cstdio> One#include <ctime> A#include <algorithm> - #definePrint_time_ printf ("Time:%f\n", double (Clock ())/clocks_per_sec) - using namespacestd; the intN,s1,v1,s2,v2; - Const intlimit=50000; -typedefLong LongLL; -LL Most_value (intNUM_S2,intS1,intV1,intS2,intV2) {//Default Enumeration s2 +LL val=0; - for(LL i=0; i<=num_s2;i++) +Val=max (Val, (N-I*S2)/s1*v1+i*v2); A at returnVal; - } - intMain () { - intT; -scanf"%d",&T); - for(intI=1; i<=t;i++){ inscanf"%d%d%d%d%d",&n,&s1,&v1,&s2,&V2); -LL value=0; to if(s1>S2) { + swap (S1, S2); - swap (V1, V2); the } * if(n/s2<=limit) { $Value=most_value (n/S2, S1, V1, S2, V2);Panax Notoginseng } - Else if(LL (S2) *v1<ll (S1) *V2) { theValue=most_value (s2-1, S2, V2, S1, V1); + } A Else{ theValue=most_value (s1-1, S1, V1, S2, V2); + } -printf"Case #%d:%lld\n", i,value); $ } $ - //print_time_; - return 0; the}
UVa 12325-zombie ' s treasure chest-[Classification enumeration]