G

                                   G - Zombie’s Treasure Chest

             Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
本题将箱子的体积分为两部分,一部分体积计算单位体积价值大的,另一部分则进行枚举求最大值
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    long long s,s1,v1,s2,v2,ans,n,m,i,t,max,L;
    int test,k;
    double k1,k2;
    cin>>test;
    for(k=1;k<=test;k++)
    {
        cin>>s>>s1>>v1>>s2>>v2;

        L=s1*s2;//
        n=s/L;//n为
        m=s%L;//这部分体积求最大值
        n=n*L;//这部分体积求单位体积大的

        k1=double(v1)/s1;
        k2=double(v2)/s2;

        if(k1>=k2)//ans保存单位体积价值大的宝石
            ans=n/s1*v1;
        else
            ans=n/s2*v2;

        if(s1<s2)
        {  swap(s1,s2);
           swap(v1,v2);
        }
        max=0;
        for(i=0;i*s1<=m;i++)//枚举(直接枚举s的话会超时)
        {
            t=i*v1+(m-i*s1)/s2*v2;
            if(max<t) max=t;
        }
        cout<<"Case #"<<k<<": "<<max+ans<<endl;
    }
    return 0;
}

 

posted @ 2013-10-11 19:28  蓝色记忆2013  阅读(208)  评论(0)    收藏  举报