2013山东省第三届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 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<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

struct node{
    long long S;
    long long P;
    double q;
}a[3];

bool cmp(node a,node b){
    return a.q>b.q;
}

int main(){

    int T;
    long long V;
    long long sum;
    long long dp[6000];
    long long tmp;
    int i,j;
    int ca=0;


    scanf("%d",&T);

    while(T--){
        scanf("%lld%lld",&a[0].S,&a[0].P);
        a[0].q=(double)(a[0].P)/a[0].S;
        //cout<<a[0].q<<endl;
        scanf("%lld%lld",&a[1].S,&a[1].P);
        a[1].q=(double)(a[1].P)/a[1].S;
        //cout<<a[1].q<<endl;
        scanf("%lld%lld",&a[2].S,&a[2].P);
        a[2].q=(double)(a[2].P)/a[2].S;
        //cout<<a[2].q<<endl;
        scanf("%lld",&V);

        sort(a,a+3,cmp);

        sum=V%a[0].S;

        for(i=1;;++i){
            if(a[0].S*i>=5000){
                break;
            }
        }

        sum=sum+a[0].S*i;

        memset(dp,0,sizeof(dp));

        for(i=0;i<3;++i){
            for(j=a[i].S;j<=sum;++j){
                tmp=dp[j-a[i].S]+a[i].P;
                if(tmp>dp[j]){
                    dp[j]=tmp;
                }
            }
        }

        for(i=sum;;--i){
            if(dp[i]>0){
                break;
            }
        }

        printf("Case %d: %lld\n",++ca,((V-sum)/a[0].S)*a[0].P+dp[i]);

    }

    return 0;
}

你可能感兴趣的:(2013山东省第三届ACM省赛 Pick apples)