The third ACM Shandong Race Pick Apples [greedy + dynamic regulation]

Source: Internet
Author: User

Big backpack problem, just beginning to want to complicate. Segmented filling backpack, a large portion of the front with the most cost-effective fill, the last part of the motion rules can be.

Title Link: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2408

Pick Apples Time limit:1000ms Memory limit:165536k in doubt. Point here ^_^ Topic 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 was so surprised because she had never seen so many apples before. Each kind of apple have a size and a price to be sold. Now the little girl wants to gain more profits, but she does isn't know how. So she asks to help, and tell she the most profits she can gain. input

The first line there are an integer t (t <=), indicates the number of test cases.
In each case, there is four lines. In the first three lines, there is both integers S and P in each line, which indicates the size (1 <= s<=) and T He price (1 <= P <= 10000) of this kind of apple.

The fourth line there are an integer V, (1 <= V <= 100,000,000) indicates the volume of the girl ' s bag. Output

For each case, first output of the case number then follow the most profits she can gain. Sample Input

11 12 13 16
Sample Output
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;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.