Pick apples(山东省第三届ACM程序设计大赛 贪心+背包)

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
Sample Output

Case 1: 6

大范围贪心,小范围背包。

#include
#include
#include
#include
using namespace std;
const int MAX_BACK=1e6;//背包的范围
typedef long long int LL;
LL dp[MAX_BACK*2+50];
typedef struct node
{
    int siz;
    int pri;
    double aver;
} nod;
nod s[10];
bool cmp(nod p,nod q)
{
    return p.aver>q.aver;
}
void comback(LL V)
{
    memset(dp,0,sizeof(dp));
    for(int i=0; i<3; i++)
    {
        for(int j=s[i].siz; j<=V; j++)
            dp[j]=max(dp[j],dp[j-s[i].siz]+s[i].pri);
    }
}
int main()
{
    int T;
    int CASE=1;
    scanf("%d",&T);
    while(T--)
    {
        LL ans=0;
        for(int i=0; i<3; i++)
        {
            scanf("%d%d",&s[i].siz,&s[i].pri);
            s[i].aver=1.0*s[i].pri/s[i].siz;
        }
        LL V;
        scanf("%lld",&V);
        if(V<=MAX_BACK)
        {
            comback(V);
            cout<<"Case "<": "<else
        {
            sort(s,s+3,cmp);
            LL x=V-MAX_BACK;
            ans=ans+(x/s[0].siz*s[0].pri);
            LL mod=x%s[0].siz;
            comback(MAX_BACK+mod);
            cout<<"Case "<": "<

你可能感兴趣的:(贪心)