第三届ACM山东省赛 Pick apples [贪心+动规]

超大背包问题,刚开始想复杂了。分段填充背包,前面一大部分用性价比最高的填充,最后一部分动规就可以了。

题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2408

 

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 "cstdio"
#include "cstring"
#include "cstdlib"
#include "algorithm"
typedef long long LL;
using namespace std;

LL dp[10240];

struct Node{
    LL sz, pri; double rat;
    bool operator < (const Node & rhs) const {
        return rat < rhs.rat;
    }
};
int main(int argc, char const *argv[])
{
    LL T, kase = 0; cin >> T;
    while(T--){
        Node v[3];
        for(LL i = 0; i < 3; ++i){
            cin >> v[i].sz >> v[i].pri;
            v[i].rat = v[i].sz * 1.0 / v[i].pri;
        }
        sort(v, v + 3);
        memset(dp, 0, sizeof(dp));
        LL all; cin >> all;
        if(all < 10000){
            for(LL i = 1; i <= all; ++i)
                for(LL j = 0; j < 3; ++j)
                    if(i - v[j].sz >= 0)
                        dp[i] = max(dp[i], dp[i-v[j].sz] + v[j].pri);
            printf("Case %lld: %lld\n", ++kase, dp[all]);
        }
        else{
            LL cnt = (all - 10000) / v[0].sz;
            all -= cnt * v[0].sz;
            for(LL i = 1; i <= all; ++i)
                for(LL j = 0; j < 3; ++j)
                    if(i - v[j].sz >= 0)
                        dp[i] = max(dp[i], dp[i-v[j].sz] + v[j].pri);
            printf("Case %lld: %lld\n", ++kase, dp[all] + cnt * v[0].pri);
        }
    }
    return 0;
}



 

posted @ 2015-04-12 21:42  Popco  阅读(184)  评论(0编辑  收藏  举报