Pick apples(山东省第三届ACM大学生程序设计竞赛)

Problem Description

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.

Input

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.

Output

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

Example Input
11 12 13 16
Example Output
Case 1: 6
 
题意:苹果园有三种苹果,你有v大小的背包,每种苹果有它的大小和价值,问怎么拿价值最大;

思路:贪心+完全背包,只有完全背包肯定不行,v太大了,超时。这样就要把v的背包分成两部分,超过1000000(三个苹果大小最大值的最小公倍数)的部分,直接贪心,拿性价比最高的就行了,但是注意,这个地方有个坑,拿性价比最高的,不一定能填满。所以要把剩下的那部分拿出来跟1000000一起做一遍完全背包,dp也要开大点;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lcm 1000000  //三个苹果大小最大值的最小公倍数;
using namespace std;
struct node
{
    int s,p;
    double sp;  //性价比;
};
long long dp[2000010],v;  //dp要开大点;
int T;
node n[5];
bool cmp(node a,node b)
{
    return a.sp>b.sp;
}
int main()
{
    int t=1;
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        for(int i=0; i<3; i++)
        {
            scanf("%d%d",&n[i].s,&n[i].p);
            n[i].sp=(1.0*n[i].p)/n[i].s;  //格式转换;
        }
        scanf("%d",&v);
        if(v<=lcm)  //小范围不用管;
        {
            for(int i=0; i<3; i++)
                for(int j=n[i].s; j<=v; j++)
                    dp[j]=max(dp[j],dp[j-n[i].s]+n[i].p);
            cout<<"Case "<<t++<<": "<<dp[v]<<endl;
        }
        else
        {
            sort(n,n+3,cmp);  //排序,直接取最优的;
            v-=lcm;  //超出lcm的部分直接贪心;
            long long sum=0;
            sum=sum+(v/n[0].s*n[0].p);
            v=lcm+(v-v/n[0].s*n[0].s);  //贪心部分不一定能整除,有余下的部分;
            for(int i=0; i<3; i++)
                for(int j=n[i].s; j<=v; j++)
                    dp[j]=max(dp[j],dp[j-n[i].s]+n[i].p);
            cout<<"Case "<<t++<<": "<<(sum+dp[v])<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值