12 algorithm experiment: pick apples (+ backpack full greedy)

Description

 

Previously, a mysterious yard which has three apples, each apple number is unlimited. There is a little girl with a big bag came to the yard, she had never seen so many apples. Each apple has the size and price of the sale, the girl wanted to get the most profit, but she did not know how to do it. So she came to you for help, you can tell her to get the maximum value of it?

 

Input

 

A first line integer T (T <= 50), indicates the number of test data sets.

Each test has four lines, three lines each have two integers S and P, respectively represent the size of each apple (1 <= S <= 100) and the price (1 <= P <= 10000)

The fourth line has an integer V (1 <= V <= 100,000,000) indicates the size of the bag girl.

 

Output

 

The maximum value of each test group the number of output and the little girl can get.

 

Sample Input

1
1 1
2 1
3 1
6

Sample Output

Case 1: 6

HINT

Backpack capacity 100 million is too big, one-dimensional array can not open.

Most will go to a capacity to install cost-effective apples, that (price / weight) the largest apple.

The remaining capacity of the backpack to use a small part of the full backpack.

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long ll;
struct Node {
    ll s,p;
    double v;
}node[4];
bool cmp(Node p,Node q) {
    return  p.v < q.v;
}
ll dp[1101];
int main()
{
    int t,v,k = 0;
    cin >> t;
    while(t--) {
        k++;
        for(int i = 0; i < 3; i++) {
            cin >> node[i].s >> node[i].p;
            node[i].v = node[i].s*1.0/node[i].p;
        }
        cin >> v;
        sort(node,node + 3,cmp);
        ll ans = 0;
        if(v > 1000) {
            ans = ((v-1000)/node[0].s)*node[0].p;
            v = 1000 + (v- 1000 )% node [ 0 ] .s;
        }
        memset(dp,0,sizeof(dp));
        for(int i = 0; i < 3; i++) {
                for(ll j = node[i].s; j <= v; j++) {
                    dp[j] = max(dp[j],dp[j-node[i].s] + node[i].p);
            }
        }
        ans += dp[v];
        cout <<"Case " << k << ": " << ans << endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/LLLAIH/p/11764258.html