Pick apples

 

原題

Pick apples

Time Limit: 1000MS    Memory limit: 165536K

題目描述

Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because she has never seen so many apples before. Each kind of apple has a size and a price to be sold. Now the little girl wants to gain more profits, but she does not know how. So she asks you for help, and tell she the most profits she can gain.

輸入

In the first line there is an integer T (T <= 50), indicates the number of test cases.
In each case, there are four lines. In the first three lines, there are two integers S and P in each line, which indicates the size (1 <= S <= 100) and the price (1 <= P <= 10000) of this kind of apple.

In the fourth line there is an integer V,(1 <= V <= 100,000,000)indicates the volume of the girl's bag.

輸出

For each case, first output the case number then follow the most profits she can gain.

示例輸入

11 12 13 16

示例輸出

Case 1: 6

 

代碼:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
long long w[5];
long long v[5];
long long dp[1000001];
struct node
{
    double pp;
    long long index;
} p[5];
int cmp(struct node a,struct node b)
{
    return a.pp>b.pp;
}
int main()
{
    long long t;
    long long m;
    scanf("%lld",&t);
    for(long long cas=1; cas<=t; cas++)
    {
        memset(dp,0,sizeof(dp));
        for(long long i=1; i<=3; i++)
        {
            scanf("%lld%lld",&w[i],&v[i]);
        }
        for(long long i=1; i<=3; i++)
        {
            p[i-1].pp=(v[i]*1.0)/(w[i]*1.0);
            p[i-1].index=i;
        }
        sort(p,p+3,cmp);
        scanf("%lld",&m);
        if(m<=1000000)
        {
            for (long long i=1; i<=m; ++i)
            {
                for (long long j=1; j<=3; ++j)
                {
                    if (i>=w[j]) dp[i]=max(dp[i],dp[i-w[j]]+v[j]);
                }
            }
            printf("Case %lld: %lld\n",cas,dp[m]);
        }
        else
        {
            long long num=(m-1000000)/w[p[0].index];
            m=m-(num+1)*w[p[0].index];
            long long sum=(num+1)*v[p[0].index];
            for (long long i=1; i<=m; ++i)
            {
                for (long long j=1; j<=3; ++j)
                {
                    if (i>=w[j]) dp[i]=max(dp[i],dp[i-w[j]]+v[j]);
                }
            }
            printf("Case %lld: %lld\n",cas,dp[m]+sum);
        }
    }
    return 0;
}
/*
1
1 1
2 1
3 1
6
*/


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章