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

  1. 3
  2. 3 2 7
  3. 2 3
  4. 4 3
  5. 1 3 2
  6. 1 2 10
  7. 3 4
  8. 2 1 2
  9. 3 2 3 1
  10. 1 2 5
  11. 1 1
  12. 2
  13. 1

Sample Output

  1. 23
  2. 45
  3. 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为前缀和。边界的地方要处理一下。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<map>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<iostream>
  7. #include<algorithm>
  8. const int maxn = 5e5+;
  9. using namespace std;
  10. typedef long long ll;
  11.  
  12. ll dp[][];
  13. ll k1,k2,c;
  14. int n,m;
  15. ll ans;
  16. ll a[],b[];
  17. ll suma[],sumb[];
  18. int main(){
  19. #ifdef LOCAL
  20. freopen("data.in","r",stdin);
  21. #endif // LOCAL
  22. int t;
  23. scanf("%d",&t);
  24. while(t--){
  25. scanf("%lld%lld%lld",&k1,&k2,&c);
  26. scanf("%d%d",&n,&m);
  27.  
  28. for(int i=;i<=n;i++) scanf("%lld",&a[i]);
  29. for(int i=;i<=m;i++) scanf("%lld",&b[i]);
  30. sort(a+,a++n);
  31. sort(b+,b++m);
  32. suma[]=;
  33. for(int i=;i<=n;i++) suma[i]=suma[i-]+a[i];
  34. sumb[]=;
  35. for(int i=;i<=m;i++) sumb[i]=sumb[i-]+b[i];
  36. ans=-;
  37. for(int i=;i<=n;i++){
  38. for(int j=;j<=m;j++){
  39. dp[i][j]=;
  40. if(i==&&j==) continue;
  41. if(i==){
  42. if(c>=sumb[j]){
  43. dp[i][j]=dp[i][j-]+k2*(c-sumb[j]);
  44. }
  45. }else if(j==){
  46. if(c>=suma[i]){
  47. dp[i][j]=dp[i-][j]+k1*(c-suma[i]);
  48. }
  49. }else{
  50. ll s = suma[i]+sumb[j];
  51. if(c>=s){
  52. dp[i][j]=max(dp[i][j-]+k2*(c-s),dp[i-][j]+k1*(c-s));
  53. }
  54. }
  55. ans=max(ans,dp[i][j]);
  56. }
  57. }
  58. cout<<ans<<endl;
  59. }
  60.  
  61. return ;
  62. }

ZOJ 4019 Schrödinger's Knapsack的更多相关文章

  1. ZOJ 4019 Schrödinger's Knapsack (from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    题意: 第一类物品的价值为k1,第二类物品价值为k2,背包的体积是 c ,第一类物品有n 个,每个体积为S11,S12,S13,S14.....S1n ; 第二类物品有 m 个,每个体积为 S21,S ...

  2. ZOJ - 4019 Schrödinger's Knapsack (背包,贪心,动态规划)

    [传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5747 [题目大意]:薛定谔的背包.薛定谔的猫是只有观测了才知道猫的死 ...

  3. zoj4019 Schrödinger's Knapsack(dp)

    题意:有两种物品分别为n,m个,每种物品对应价值k1,k2.有一个容量为c的背包,每次将一个物品放入背包所获取的价值为k1/k2*放入物品后的剩余体积.求问所获取的最大价值. 整体来看,优先放入体积较 ...

  4. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  5. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  6. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  7. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  8. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  9. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

随机推荐

  1. HTML使用button的一个小坑

    https://www.w3schools.com/TAGs/att_button_type.asp Definition and Usage The type attribute specifies ...

  2. 关于js中this指向的理解总结!

    关于js中this指向的理解! this是什么?定义:this是包含它的函数作为方法被调用时所属的对象. 首先,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁 ...

  3. [转帖]DevOps/TestOps概念

    发现收藏不好用..还是转吧.. https://www.cnblogs.com/fnng/p/8232410.html DevOps/TestOps概念 2018-01-07 22:02 by 虫师, ...

  4. link & auto cards

    link & auto cards a link to card link https://docs.embed.ly/docs/cards DD WX <blockquote clas ...

  5. MyBatis:传参

    MyBatis从入门到放弃二:传参 前言 我们在mapper.xml写sql,如果都是一个参数,则直接配置parameterType,那实际业务开发过程中多个参数如何处理呢? 从MyBatis API ...

  6. zookeeper 四字命令

    zookeeper四字命令   ZooKeeper3.4.6支持某些特定的四字命令字母与其的交互.它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息.用户在客户端可以通过 te ...

  7. LOJ6045 雅礼集训 2017 Day8 价(最小割)

    由Hall定理,任意k种减肥药对应的药材数量>=k.考虑如何限制其恰好为k,可以将其看作是使对应的药材数量尽量少. 考虑最小割.建一个二分图,左边的点表示减肥药,右边的点表示药材.减肥药和其使用 ...

  8. EF 更新 删除

    为了避免先查询后更新或删除的问题 可以使用如下语句 Entities db = new Entities(); Orders o = new Orders(); o.id = 6; o.name = ...

  9. zxing生成二维码设置边框颜色

    真是研究了很久很久,满满的泪啊 zxing生成二维码,默认是可以增加空白边框的,但是并没有可以设置边框颜色的属性. 其中增加空白边框的属性的一句话是: Map hints = new HashMap( ...

  10. MT【16】证明无理数(2)

    证明:$sin10^0$为无理数. 分析:此处用$sin$的三倍角公式,结合多项式有有理根必须满足的系数之间的关系可以证明. 评:证明$sin9^0$为无理数就不那么简单.思路:先利用$sin54^0 ...