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;
    }
    



  • 相关阅读:
    Tsinghua 2018 DSA PA3简要题解
    Tsinghua 2018 DSA PA2简要题解
    Python logging系统
    Surface RT2使用情况
    隔壁信概大作业xjb写——同化棋ATAXX
    XJTUOJ #1080 qz的不卡常数
    XJTUOJ #1081 JM的赃物被盗
    XJTUOJ #1078 JM的恶有恶报
    洛谷P5425 [USACO19OPEN]I Would Walk 500 Miles G
    洛谷P2857 [USACO06FEB]Steady Cow Assignment G
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312670.html
Copyright © 2011-2022 走看看