zoj4019(dp)

Schrödinger's Knapsack

Time Limit: 1 Second      Memory Limit: 65536 KB

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.



题意:背包问题,但是放的顺序会影响结果。每次放入一物品,其获得的值都可以用v=kr计算,r表示放入后背包剩下的容量,有两种物品,k分别为k1,k2,有大小各不一的这两种物品若干,放入容量为c的背包中,能获得求最大的值。

思路:先放小的优于先放大的,两种物品大小都sort一下,dp[i][j]表示第一种从前j个个中选,第二种从前i个中选后的最大结果。

PS:不要memset这个dp,会tle!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2005],b[2005];
ll suma[2005],sumb[2005];
ll dp[2005][2005];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        // memset(dp,0,sizeof(dp));
        memset(suma,0,sizeof(suma));
        memset(sumb,0,sizeof(sumb));
        ll k1,k2,c;
        scanf("%lld%lld%lld",&k1,&k2,&c);
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
        for(int i=1;i<=m;i++)scanf("%lld",&b[i]);
        sort(a+1,a+n+1);
        sort(b+1,b+m+1);
        for(int i=1;i<=n;i++)
        {
            suma[i]=suma[i-1]+a[i];
        }
        for(int i=1;i<=m;i++){
        
            sumb[i]=sumb[i-1]+b[i];
        }
        ll ans=0;
        dp[0][0]=0;
        for(int i=1;i<=n;i++)
        {
            if(suma[i]<=c)
            {
                dp[0][i]=dp[0][i-1]+k1*(c-suma[i]);
                ans=max(ans,dp[0][i]);
            }
            else dp[0][i]=-1;
        }
        for(int i=1;i<=m;i++)
        {
            if(sumb[i]<=c)
            {
                dp[i][0]=dp[i-1][0]+k2*(c-sumb[i]);
                ans=max(ans,dp[i][0]);
            }
            else dp[i][0]=-1;
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(dp[i-1][j]==-1&&dp[i][j-1]==-1)
                {
                    dp[i][j]=-1;
                }
                if(dp[i-1][j]==-1)
                {
                    dp[i][j]=dp[i][j-1]+k1*(c-sumb[i]-suma[j]);
                }
                else if(dp[i][j-1]==-1)
                {
                    dp[i][j]=dp[i-1][j]+k2*(c-sumb[i]-suma[j]);
                }
                else 
                {
                    dp[i][j]=max(dp[i-1][j]+k2*(c-sumb[i]-suma[j]),dp[i][j-1]+k1*(c-sumb[i]-suma[j]));
                }
                ans=max(ans,dp[i][j]);
            }
        }
        printf("%lld\n",ans);
    }
}





### 关于ZOJ 2193题目描述与解决方案 对于ZOJ 2193的具体题目描述未能直接从当前提供的参考资料中找到对应的信息。然而,在处理此类竞赛编程问题时,通常遵循一定的模式来解析和解决。 #### 动态规划的应用场景 当面对涉及序列优化的问题时,动态规划是一种常见的有效方法[^1]。虽然此原则适用于许多算法挑战,但对于特定编号为2193的ZOJ问题而言,具体的实现细节会有所不同。 #### 数据结构的选择 针对某些类型的查询操作频繁的数据集,采用合适的数据结构可以显著提高效率。例如,在处理区间更新或查询的情况下,线段树是一个强有力的选择[^4]。不过这取决于具体问题的需求,并不一定适合ZOJ 2193。 #### 数学模型构建的重要性 一些离散数学概念如组合计数、概率论以及特殊情况下像约瑟夫环这样的经典问题能够帮助建立有效的数学模型来进行求解[^2]。尽管这些技巧广泛应用于各类编程比赛中,它们是否能用于解答ZOJ 2193还需进一步确认该题目的具体内容。 为了获得关于ZOJ 2193更精确的帮助,建议访问原网站获取官方文档中的确切表述或者查阅其他参赛者分享的经验贴以了解可能存在的高效解法路径。 ```python # 此处提供一个通用框架作为参考而非特指ZOJ 2193的答案 def solve_problem(input_data): # 假设输入数据已经预处理完成并存储在input_data变量里 result = [] # 存储最终的结果列表 n = len(input_data) dp = [0]*n # 初始化dp数组用来记录状态转移过程中的最优子结构信息 for i in range(n): # 更新dp[i], 这里的逻辑应该基于实际问题定义而定 pass return result ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值