Pick apples(完全背包+贪心)

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.

Sample

Input

1
1 1
2 1
3 1
6

Output

Case 1: 6

Hint

Source

2012年

#include <stdio.h>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
/*小范围完全背包,大范围贪心*/
long long dp[1100009];
struct node
{
    int a,b;
    double c;//体积/价格
} a[4];
bool cmp(node a,node b)
{
    return a.c<b.c;
}
long long max1(long long a,long long b)
{
    return a>b?a:b;
}
int main()
{
    int t,case1=1;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        long long v;
        for(int i=1; i<=3; i++)
        {
            scanf("%d%d",&a[i].a,&a[i].b);
            a[i].c=a[i].a*1.0/a[i].b;//计算体积/钱
        }
        cin>>v;
        sort(a+1,a+4,cmp);//数组a是从1开始被输入的
        long long ans =0;
        if(v>1000000)
        {
            ans=(((v-1000000)/a[1].a)*a[1].b);//v-1000000是因为想用贪心的办法把那(v-1000000)个位置填满,免得超时(体积太大)
            v=1000000+((v-1000000)%a[1].a);//计算(v-1000000)后剩余的空间
        }
        for(int i=1; i<=3; i++)
            for(long long j=a[i].a; j<=v; j++)//用完全背包计算这个能装多少
            dp[j]=max1(dp[j],dp[j-a[i].a]+a[i].b);
        printf("Case %d: %lld\n",case1++,ans+dp[v]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/RootVount/p/10651430.html

时间: 2024-11-06 14:08:08

Pick apples(完全背包+贪心)的相关文章

[2012山东ACM省赛] 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

[2012山东ACM省赛] 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 s

集训第四周(高效算法设计)L题 (背包贪心)

Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of songs on his tapes. For a given tape and for each song on that tape John knows the length of the song and the frequency of playing that song. His probl

sdut2408 pick apples (贪心+背包)山东省第三届ACM省赛

本文出自:http://blog.csdn.net/svitter/ 题意:三种苹果,每种都有对应的Size,Value,给你一个背包空间,求最大的价值. 本题目的关键就在于非常大的背包空间 依据indicates the size (1 <= S<= 100) 我们可以考虑在1000000(100^3)之外的空间放性价比最高的苹果.为什么时100^3? 要知道背包如果正好填满,而填满空间相应价值的苹果大于不填满的价值的苹果,那么就选择能填满空间而使价值最大的苹果,而非性价比最高的苹果--性价

多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)

题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: 首先我们能够(ˇ?ˇ) 想-,假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个圈劈开.那么对于每一个区间由于苹果数非常少,所以能够利用belong[x]数组记录每一个苹果所在的苹果树位置,然后将苹果依照所在的位置排序,那么也就是我们知道每次拿k个苹果的代价是苹果所在的最远

Delicious Apples (hdu 5303 贪心+枚举)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 395    Accepted Submission(s): 122 Problem Description There are n apple trees planted along a cyclic road, which is L metres

2015南阳CCPC D - Pick The Sticks 背包DP.

D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting

饭卡(01背包+贪心)

电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家都希望尽量使卡上的余额最少. 某天,食堂中有n种菜出售,每种菜可购买一次.已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少. Input多组数据.对于每组数据: 第一行为正整数n,表示菜的数量.n<=1000. 第二行包括n个正整数,表示每种菜的价格.价格不超过50. 第三行包括一个正整数

HDU -2546饭卡(01背包+贪心)

这道题有个小小的坎,就是低于5块不能选,大于5块,可以任意选,所以就在初始条件判断一下剩余钱数,然后如果大于5的话,这时候就要用到贪心的思想,只要大于等于5,先找最大的那个,然后剩下的再去用背包去选择,这样的结果一定是最优的.因为最大的那个一定会被选中,剩下多少钱都无所谓,用背包可以获得剩下的最优解,所以最后也是最优解 代码如下 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using