ZOJ 4019 Schrödinger's Knapsack

Posted litos

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ 4019 Schrödinger's Knapsack相关的知识,希望对你有一定的参考价值。

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.


Author: CHEN, Shihan
Source: The 18th Zhejiang University Programming Contest Sponsored by TuSimple

 

题目链接

题意 

有个容量为c的背包,两类物品,其权值分别为k1、k2,第一种物品有n个,第二种有m个,每个物体都有自己的体积。当放进一个物体进入背包时,其获得的价值的对应的权值k乘上放入当前物体后剩余的容量。现在问,能够获得的最大价值是多少?

 

分析 

显然,对于同一类物品,取其体积最小的几个是最划算的,所以先排序。那么根据这个来定义状态dp[i]][j]为选了第一种前i个、第二种前j个,最小的前i个和前j个是必选的,那么此时的容量我们能够计算出来。状态转移也很显然,dp[i][j]=max(dp[i-1][j],dp[i][j-1]) (c-suma[i]-sumb[j]>0),其中suma和sumb为前缀和。边界的地方要处理一下。

 

 

#include<cstdio>
#include<cstring>
#include<map>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
const int maxn = 5e5+5;
using namespace std;
typedef long long ll;

ll dp[2020][2020];
ll k1,k2,c;
int n,m;
ll ans;
ll a[2020],b[2020];
ll suma[2020],sumb[2020];
int main(){
#ifdef LOCAL
    freopen("data.in","r",stdin);
#endif // LOCAL
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld",&k1,&k2,&c);
        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+1+n);
        sort(b+1,b+1+m);
        suma[0]=0;
        for(int i=1;i<=n;i++) suma[i]=suma[i-1]+a[i];
        sumb[0]=0;
        for(int i=1;i<=m;i++) sumb[i]=sumb[i-1]+b[i];
        ans=-1;
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                dp[i][j]=0;
                if(i==0&&j==0) continue;
                if(i==0){
                    if(c>=sumb[j]){
                        dp[i][j]=dp[i][j-1]+k2*(c-sumb[j]);
                    }
                }else if(j==0){
                    if(c>=suma[i]){
                        dp[i][j]=dp[i-1][j]+k1*(c-suma[i]);
                    }
                }else{
                    ll s = suma[i]+sumb[j];
                    if(c>=s){
                        dp[i][j]=max(dp[i][j-1]+k2*(c-s),dp[i-1][j]+k1*(c-s));
                    }
                }
                ans=max(ans,dp[i][j]);
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

 

以上是关于ZOJ 4019 Schrödinger's Knapsack的主要内容,如果未能解决你的问题,请参考以下文章

一维谐振子定态 Schrödinger 方程的数值解法

数学王子--Gauss

错误 MSB4019 - MSBUILD 正在寻找不存在版本的导入

通过CD4019设计的电子时钟

npm install -g karma error MSB4019: 未找到导入的项目“C:\Microsoft.Cpp.Default.props”

外部VS2013构建错误“错误MSB4019:未找到导入的项目<path>”