ACboy needs your help

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4491    Accepted Submission(s): 2403

Problem Description
ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the profit?
 
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.

Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].

N = 0 and M = 0 ends the input.
 
Output
For each data set, your program should output a line which contains the number of the max profit ACboy will gain.
 
Sample Input
  1. 2 2
  2. 1 2
  3. 1 3
  4. 2 2
  5. 2 1
  6. 2 1
  7. 2 3
  8. 3 2 1
  9. 3 2 1
  10. 0 0
 
Sample Output
  1. 3
  2. 4
  3. 6
 
Source
 

背包九讲中已经很好的介绍了这种问题:

复杂度(n*m*m)

泛化物品

定义 

考虑这样一种物品,它并没有固定的费用和价值,而是它的价值随着你分配给它的费用而变化。这就是泛化物品的概念。 



更严格的定义之。在背包容量为V的背包问题中,泛化物品是一个定义域为0..V中的整数的函数h,当分配给它的费用为v时,能得到的价值就是h(v)。 



这个定义有一点点抽象,另一种理解是一个泛化物品就是一个数组h[0..V],给它费用v,可得到价值h[V]。 



一个费用为c价值为w的物品,如果它是01背包中的物品,那么把它看成泛化物品,它就是除了h(c)=w其它函数值都为0的一个函数。如果它是完全背包中的物品,那么它可以看成这样一个函数,仅当v被c整除时有h(v)=v/c*w,其它函数值均为0。如果它是多重背包中重复次数最多为n的物品,那么它对应的泛化物品的函数有h(v)=v/c*w仅当v被c整除且v/c<=n,其它情况函数值均为0。 



一个物品组可以看作一个泛化物品h。对于一个0..V中的v,若物品组中不存在费用为v的的物品,则h(v)=0,否则h(v)为所有费用为v的物品的最大价值。P07中每个主件及其附件集合等价于一个物品组,自然也可看作一个泛化物品。 



泛化物品的和 

如果面对两个泛化物品h和l,要用给定的费用从这两个泛化物品中得到最大的价值,怎么求呢?事实上,对于一个给定的费用v,只需枚举将这个费用如何分配给两个泛化物品就可以了。同样的,对于0..V的每一个整数v,可以求得费用v分配到h和l中的最大价值f(v)。也即f(v)=max{h(k)
+l(v-k)|0<=k<=v}。可以看到,f也是一个由泛化物品h和l决定的定义域为0..V的函数,也就是说,f是一个由泛化物品h和 l决定的泛化物品。 



由此可以定义泛化物品的和:h、l都是泛化物品,若泛化物品f满足f(v)=max{h(k)+l(v-k)|0<=k<=v},则称f是h与l的和,即f=h+l。这个运算的时间复杂度是O(V^2)。

泛化物品的定义表明:在一个背包问题中,若将两个泛化物品代以它们的和,不影响问题的答案。事实上,对于其中的物品都是泛化物品的背包问题,求它的答案的过程也就是求所有这些泛化物品之和的过程。设此和为s,则答案就是s[0..V]中的最大值。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <string>
  10. #define oo 0x13131313
  11. using namespace std;
  12. int n,m;
  13. int A[101][101];
  14. void input()
  15. {
  16. for(int i=1;i<=n;i++)
  17. for(int j=1;j<=m;j++)
  18. scanf("%d",&A[i][j]);
  19. }
  20. void init()
  21. {
  22. freopen("a.in","r",stdin);
  23. freopen("a.out","w",stdout);
  24. }
  25. void ADD(int *ans,int *a,int *b)
  26. {
  27. for(int i=0;i<=m;i++)
  28. {
  29. ans[i]=0;
  30. for(int j=0;j<=i;j++)
  31. {
  32. ans[i]=max(ans[i],a[j]+b[i-j]);
  33. }
  34. }
  35. }
  36. void solve()
  37. {
  38. int ans=0;
  39. if(n>=2)
  40. {
  41. ADD(A[0],A[1],A[2]);
  42. for(int i=3;i<=n;i++)
  43. ADD(A[i-2],A[i-3],A[i]);
  44. for(int i=1;i<=m;i++)
  45. ans=max(ans,A[n-2][i]);
  46. printf("%d\n",ans);
  47. }
  48. else
  49. {
  50. for(int i=1;i<=m;i++)
  51. ans=max(ans,A[1][i]);
  52. printf("%d\n",ans);
  53. }
  54. }
  55. int main()
  56. {
  57. // init();
  58. while(cin>>n>>m&&(n||m))
  59. {
  60. input();
  61. solve();
  62. }
  63. }

【泛化物品】【HDU1712】【ACboy needs your help】的更多相关文章

  1. HDU1712:ACboy needs your help(分组背包模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem Description ACboy has N courses this term, an ...

  2. P1757 通天之分组背包 / hdu1712 ACboy needs your help (分组背包入门)

    P1757 通天之分组背包 hdu1712 ACboy needs your help hdu1712题意:A[i][j]表示用j天学习第i个课程能够得到A[i][j]的收益,求m天内获得的收益最大值 ...

  3. BZOJ4978: [Lydsy1708月赛]泛化物品(乱搞)

    4978: [Lydsy1708月赛]泛化物品 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 220  Solved: 70[Submit][Statu ...

  4. hdu1712 ACboy needs your help 分组背包

    最基础的分组背包~ #include <iostream> #include <cstdio> #include <cstdlib> #include <cs ...

  5. 分组背包----HDU1712 ACboy needs your help

    很简单的一道分组背包入门问题.不多解释了. #include <iostream> #include <cstdio> #include <cstring> usi ...

  6. [hdu1712]ACboy needs your help分组背包

    题意:一共$m$天,$n$门课程,每门课程花费$i$天得到$j$的价值,求最后获得的最大价值 解题关键:分组背包练习,注意循环的顺序不能颠倒 伪代码: $for$ 所有的组$k$   $for{\rm ...

  7. Battle Ships(复习泛化物品**)

    传送门Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which i ...

  8. HDU1712 ACboy needs your help(分组背包)

    每种课程学习不同天数可以获得不同价值,这可以看成一个组,那么题目就是分组背包的模板题了. 1 #include<cstdio> 2 #include<cstring> 3 #i ...

  9. HDU 1712 ACboy needs your help(分组背包入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意: 有个人学习n门课程,a[i][j]表示用j分钟学习第i门课程所能获得的价值,背包容量为一共有m时间 ...

随机推荐

  1. 3 x 8 = 23(火了)

    颜回爱学习,德性又好,是孔子的得意门生.一天,颜回去街上办事,见一家布店前围满了人.他上前一问,才知道是买布的跟卖布的发生了纠纷. 只听买布的大嚷大叫:「三八就是二十三,你为啥要我二十四个钱?」颜回走 ...

  2. 在web应用程序中使用MemcachedClient

    本文来自:http://www.cnblogs.com/yukaizhao/archive/2008/11/10/memcached_client_usage.html 一. 背景: 在大访问量的we ...

  3. ModelAndView解析

    查看spring的帮助文档得到下面信息: org.springframework.web.servlet Class ModelAndViewjava.lang.Object org.springfr ...

  4. 关于Node.js, Jade一点小小的介绍。

    本文出自:http://blog.csdn.net/svitter node.js大家知道的可能比較多,可是jade大家可能就不知道了.. GFW封杀掉google以后.今天在百度上找了好久也没有找到 ...

  5. 在EasyUI中统一判断是否有选中行,如果有则将选中行数据传入回调函数

    function procossWithSeletedData(func) { var rowData = $("#tbGrid").datagrid("getSelec ...

  6. C#面试题总结——程序设计基础

    一.类型与变量 1.C#支持哪几个预定义的值类型? 主要包括五个类型:整数,浮点数,字符型,bool类型以及decimal型(小数型).其中每一个类型分别有多个内置类型组成. 2.C#支持哪几个预定义 ...

  7. MFC 控件初始化的过程

    之前为了学习MFC下浏览器的用法,参考博文:http://www.cnblogs.com/firefly_liu/archive/2009/05/18/1459514.html,虽然按照作者的方法实现 ...

  8. web在线聊天系统。非ajax轮询

    利用php的死循环和刷新缓冲区实现.   浏览器发送请求到PHP获取消息页面. php接收到来之浏览器请求后. 循环获取数据库里面的消息.   当存在消息的时候.PHP告诉浏览器.我有消息给你.你接受 ...

  9. CSS 怀疑 Verify

    1. height 之前好像看到有人说,div标签的height设置一个固定值后,div标签的height会随着内容的增多而变大 经测试,内容会溢出div标签,但div标签的height不会随着变化

  10. Interaction with the camera or the photo library

    As we said before, we need a delegate to deal with the user interaction with the camera or the photo ...