第18届浙江大学校赛 F Schrödinger's Knapsack (DP)

DreamGrid has a magical knapsack with a size capacity of  called the Schrödinger's knapsack (or S-knapsack for short) and two types of magical items called the Schrödinger's items (or S-items for short). There are  S-items of the first type in total, and they all have a value factor of ; While there are  S-items of the second type in total, and they all have a value factor of . The size of an S-item is given and is certain. For the -th S-item of the first type, we denote its size by ; For the -th S-item of the second type, we denote its size by .

But the value of an S-item remains uncertain until it is put into the S-knapsack (just like Schrödinger's cat whose state is uncertain until one opens the box). Its value is calculated by two factors: its value factor , and the remaining size capacity  of the S-knapsack just after it is put into the S-knapsack. Knowing these two factors, the value  of an S-item can be calculated by the formula .

For a normal knapsack problem, the order to put items into the knapsack does not matter, but this is not true for our Schrödinger's knapsack problem. Consider an S-knapsack with a size capacity of 5, an S-item with a value factor of 1 and a size of 2, and another S-item with a value factor of 2 and a size of 1. If we put the first S-item into the S-knapsack first and then put the second S-item, the total value of the S-items in the S-knapsack is ; But if we put the second S-item into the S-knapsack first, the total value will be changed to . The order does matter in this case!

Given the size of DreamGrid's S-knapsack, the value factor of two types of S-items and the size of each S-item, please help DreamGrid determine a proper subset of S-items and a proper order to put these S-items into the S-knapsack, so that the total value of the S-items in the S-knapsack is maximized.

Input

The first line of the input contains an integer  (about 500), indicating the number of test cases. For each test case:

The first line contains three integers  and  (), indicating the value factor of the first type of S-items, the value factor of the second type of S-items, and the size capacity of the S-knapsack.

The second line contains two integers  and  (), indicating the number of the first type of S-items, and the number of the second type of S-items.

The next line contains  integers  (), indicating the size of the S-items of the first type.

The next line contains  integers  (), indicating the size of the S-items of the second type.

It's guaranteed that there are at most 10 test cases with their  larger than 100.

Output

For each test case output one line containing one integer, indicating the maximum possible total value of the S-items in the S-knapsack.

Sample Input
3
3 2 7
2 3
4 3
1 3 2
1 2 10
3 4
2 1 2
3 2 3 1
1 2 5
1 1
2
1
Sample Output
23
45
10
Hint

For the first sample test case, you can first choose the 1st S-item of the second type, then choose the 3rd S-item of the second type, and finally choose the 2nd S-item of the first type. The total value is .

For the second sample test case, you can first choose the 4th S-item of the second type, then choose the 2nd S-item of the first type, then choose the 2nd S-item of the second type, then choose the 1st S-item of the second type, and finally choose the 1st S-item of the first type. The total value is .

The third sample test case is explained in the description.

It's easy to prove that no larger total value can be achieved for the sample test cases.

题意:
  第一类物品的价值为k1,第二类物品价值为k2,背包的体积是 c ,第一类物品有n 个,每个体积为S11,S12,S13,S14.....S1n ; 第二类物品有 m 个,每个体积为 S21,S22,S23,S24.......S2m;每次装入物品时,得到的价值是 剩余背包体积*该类物品的价值;问最多能得到的总价值是多少
思路:

  要想得到最大的总价值,肯定要从小的开始装,然后分别枚举第一类,第二类装进去的最大体积,还有将两类回合装入背包的最大体积,得到最后的答案用dp[i][j],来表示 1 - i 的第一种物品区间,1 - j 的第二种物品区间,即装入 1到 i 的一类物品和 1 到 j 的二类物品的所得到的最大价值

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
typedef long long ll;

int t;
ll dp[maxn][maxn];
ll c1, c2, c;
ll v1[maxn], v2[maxn], sum1[maxn], sum2[maxn];
//我们用dp[i][j],来表示 1 - i 的第一种物品区间,
//1 - j 的第二种物品区间,即装入 1到 i 的一类物品和
// 1 到 j 的二类物品的所得到的最大价值
int main()
{
    cin>>t;
    while(t--){
        cin>>c1>>c2>>c;
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            cin>>v1[i];
        for(int i=1;i<=m;i++)
            cin>>v2[i];
        sort(v1+1,v1+1+n);
        sort(v2+1,v2+1+m);
        for(int i=1;i<=n;i++)
            sum1[i]=sum1[i-1]+v1[i];
        for(int i=1;i<=m;i++)
            sum2[i]=sum2[i-1]+v2[i];
       for (int i=0;i<=n;i++)
        for (int j=0;j<=m;j++)
            dp[i][j]=0;
        ll ans=0;
        //只装第一种
        for(int i=1;i<=n;i++){
            if(sum1[i]<c){
                dp[i][0]=c1*(c-sum1[i])+dp[i-1][0];
                ans=max(ans,dp[i][0]);
            }
        }
        //只装第二种
        for(int j=1;j<=m;j++){
            if(sum2[j]<c){
                dp[0][j]=c2*(c-sum2[j])+dp[0][j-1];
                ans=max(ans,dp[0][j]);
            }
        }
        //两种均装
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            ll cnt=sum1[i]+sum2[j];
            if(sum1[i]+sum2[j]<=c){
                dp[i][j]=max(dp[i-1][j]+c1*(c-cnt),dp[i][j-1]+c2*(c-cnt));
                ans=max(ans,dp[i][j]);
            }
        }
        cout<<ans<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值