Zombie’s Treasure Chest | ||||||
| ||||||
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 | ||||||
Sample Output | ||||||
Case #1: 100 Case #2: 86 | ||||||
Source | ||||||
2011 Asia Shanghai Regional Contest |
题目大意:
现在给你一个容量为a的背包,有两种物品,一种是重量为b价值为c的物品,一种是重量为d,价值为e的物品,问怎样装物品能够使得价值最大。
思路:
其实就是一个暴力枚举的题,我们贪心的想要装单位价值比高的物品,那么尽量全拿这个价值比高的物品剩余的容量去撞价值低的物品的话,可能空间会有剩余,会有如果价值比高的物品稍微少拿两件会更优的情况。
那么我们枚举贪心之后少拿的个数即可。
暴力枚举到1e5不会TLE.
Ac代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define ll long long int
ll output;
void Slove(ll a,ll b,ll c,ll d,ll e)
{
ll num1=a/b;
ll num2=(a-(a/b*b))/d;
ll yu=(a-(a/b*b))%d;
output=max(output,num1*c+num2*e);
for(ll i=0; i<=100000; i++)
{
if(num1-i>=0)
{
ll tmp1=num1-i;
ll money=i*b+yu;
ll tmp2=num2+money/d;
ll sum=tmp1*c+tmp2*e;
output=max(output,sum);
}
}
}
int main()
{
ll kase=0;
ll t;
scanf("%lld",&t);
while(t--)
{
ll a,b,c,d,e;
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&e);
output=0;
Slove(a,b,c,d,e);
Slove(a,d,e,b,c);
printf("Case #%lld: %lld\n",++kase,output);
}
}