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
2 2
1 2
1 3
2 2
2 1
2 1
2 3
3 2 1
3 2 1
0 0
 
Sample Output
3
4
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]中的最大值。

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
int n,m;
int A[101][101];
void input()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&A[i][j]);
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
void ADD(int *ans,int *a,int *b)
{
for(int i=0;i<=m;i++)
{
ans[i]=0;
for(int j=0;j<=i;j++)
{
ans[i]=max(ans[i],a[j]+b[i-j]);
}
}
}
void solve()
{
int ans=0;
if(n>=2)
{
ADD(A[0],A[1],A[2]);
for(int i=3;i<=n;i++)
ADD(A[i-2],A[i-3],A[i]);
for(int i=1;i<=m;i++)
ans=max(ans,A[n-2][i]);
printf("%d\n",ans);
}
else
{
for(int i=1;i<=m;i++)
ans=max(ans,A[1][i]);
printf("%d\n",ans);
}
}
int main()
{
// init();
while(cin>>n>>m&&(n||m))
{
input();
solve();
}
}

【泛化物品】【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. Spring定时任务的几种实现(转自iteye网gong1208)

    原文地址: http://gong1208.iteye.com/blog/1773177 以下为正文: 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整 ...

  2. JavaScript 轮播图

    这是我自己做的一个轮播图,大家可以看看 ,我还没进行优化.有改进的地方可以私聊 布局什么的你们自己搞定吧 <div class="slider" id="circl ...

  3. Pasha and String(思维,技巧)

    Pasha and String Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  4. MHA自动切换流程

    MHA的全名叫做mysql-master-ha,配置后可以在10-30秒内完成master自动切换,切换过程如下: 1. 检测master的状态,方法是一秒一次“ SELECT 1 As Value” ...

  5. Git使用过程

    Git-------目前世界上最先进的分布式版本控制系统(没有之一) 什么是版本控制系统? 说简单点,就是一个文件,对其增加.删除.修改都可以被记录下来,不仅自己可以修改,其他人也可以进行修改 每次对 ...

  6. 使用 Camtasia Recorder显示 “ camtasia an error occurred in the recorder: video codec open failed ”

    这是因为本机没有codec的缘故,可以下载一个:tscc解码器(TechSmith Screen Capture Codec) 2.0.3.0 安装版 http://www.cngr.cn/dir/2 ...

  7. flex与js相互调用

    1.flex调用js方法 调用方法例如:ExternalInterface.call("UploadComplete",oldName,uidName,_dir+"/&q ...

  8. Oracle sql语言模糊查询--like后面的通配符

    关于like后面的条件,Oracle提供了四种匹配模式: 1,% :表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FR ...

  9. 使用xib方式创建UITableViewCell,设置Label自动换行注意事项

    自定义的UITableViewCell,使用xib方式创建,想要其中的UILabel换行显示:计算Label的高度,让其自动换行,总是没有效果. 我猜测原因可能在于使用了autolayout布局.只要 ...

  10. HDU 3306 - Another kind of Fibonacci

    给你 A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2). 求 S(N) = A(0) 2 +A(1) 2+……+ ...