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. 【刷题 Python Tip】题目6~10

    [题目6]输出100以内的所有素数,素数之间以一个空格区分 from math import sqrt print ' '.join(str(key) for key in [x for x in x ...

  2. 改变UITableViewCell按下去的颜色

    UIView *bgColorView = [[UIView alloc] init]; [bgColorView setBackgroundColor:[UIColor redColor]]; [c ...

  3. poj 2774 最长公共子--弦hash或后缀数组或后缀自己主动机

    http://poj.org/problem?id=2774 我想看看这里的后缀数组:http://blog.csdn.net/u011026968/article/details/22801015 ...

  4. The 2013 South America/Brazil Regional Contest 题解

    A: UVALive 6525 cid=61196#problem/A" style="color:blue; text-decoration:none">Atta ...

  5. ADB命令解析

    ADB全称Android Debug Bridge, 是android sdk里的一个工具, 用这个工具可以直接操作管理android模拟器或者真实的andriod设备(手机). 它的主要功能有: * ...

  6. unity tips

    1.在unity 的mecanim中,如果一个动画指向两个或两个以上的动画,那么在inspector中,transitions中可以看到所有的过渡路径,这些路径是有先后顺序的.

  7. yum安装epel库后,安装软件总是提示Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again(无法检索epel仓库)

    解决方法: vi /etc/yum.repos.d/epel.repo 将baseurl中的注释取消,将mirrorlist该列注释掉即可. 附图如下:

  8. VLC各个Module模块之间共享变量的实现方法

    在做VLC开发的时候,想使用一个模块访问另外一个模块的数据, 比如在网络模块得到了一些数据,想在其他模块得到这些数据进行处理,这时候就需要两个模块共享一些变量. 查看VLC的源码,发现VLC专门有va ...

  9. FCKeditor

    FCKeditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器.它志于轻量化,不需要太复杂的安装步骤即可使用.     它可和PHP.JavaScript.ASP.ASP.NET.Col ...

  10. 自动匹配HTTP请求中对应实体参数名的数据(性能不是最优)

    /// <summary> /// 获取请求参数字段 /// </summary> /// <typeparam name="T"></t ...