zoukankan      html  css  js  c++  java
  • 第三届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
    ", ++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
    ", ++kase, dp[all] + cnt * v[0].pri);
            }
        }
        return 0;
    }
    



  • 相关阅读:
    总结Spring Set注入方式及对property标签的理解
    spring基于Annotation装配Bean时在bean.xml中添加<context:component-scan>标签报错
    java web(struts2)+python+mysql 的简单实践
    罗辑思维--怎样成为一个高手
    教练助手
    小组作业(第五组)
    个人开发总结
    第五组小组作业
    个人作业
    小组总结
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312670.html
Copyright © 2011-2022 走看看