描述:

  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?

  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.

  For each data set, your program should output a line which contains the number of the max profit ACboy will gain.

代码:

  多重背包问题,不要求装满背包。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define MAX 105 int main(){
int n,m,dp[MAX][MAX],value[MAX][MAX],max;
while( scanf("%d%d",&n,&m)!=EOF && n!= && m!= ){
memset(value,,sizeof(value));
for( int i=;i<=n;i++ )
for( int j=;j<=m;j++ )
scanf("%d",&value[i][j]);
memset(dp,,sizeof(dp));//并未要求背包放满 for( int i=;i<=n;i++ ){
for( int j=;j<=m;j++ ){
max=;
for( int k=;k<=j;k++ )//第i个物品放0-j个
max=(max>dp[i-][j-k]+value[i][k])?max:dp[i-][j-k]+value[i][k];
dp[i][j]=max;//背包容量为j,放前i个物品得到的最大值
}
}
printf("%d\n",dp[n][m]);
}
system("pause");
return ;
}

  空间复杂度优化。可以看出,当计算dp[i][j]时,用到的数值为dp[i-1][0]到dp[i-1][j]的值,故背包容量的遍历顺序需反序,可以将dp二维数组优化为一维。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define MAX 105 int main(){
int n,m,dp[MAX],value[MAX][MAX],max;
while( scanf("%d%d",&n,&m)!=EOF && n!= && m!= ){
memset(value,,sizeof(value));
for( int i=;i<=n;i++ )
for( int j=;j<=m;j++ )
scanf("%d",&value[i][j]);
memset(dp,,sizeof(dp));//并未要求背包放满 for( int i=;i<=n;i++ ){
for( int j=m;j>=;j-- ){
max=;
for( int k=;k<=j;k++ )//第i个物品放0-j个
max=(max>dp[j-k]+value[i][k])?max:dp[j-k]+value[i][k];
dp[j]=max;//背包容量为j,放前i个物品得到的最大值
}
}
printf("%d\n",dp[m]);
}
system("pause");
return ;
}

HDU1712-ACboy needs your help的更多相关文章

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

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

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

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

  3. hdu1712 ACboy needs your help 分组背包

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

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

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

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

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

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

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

  7. HZNU-ACM寒假集训Day7小结 背包DP

    背包问题 01背包 状态:f(i,j) 表示只能装前i个物品的情况下,容量为j的背包所能达到的最大总价值 状态转移方程:  f(i,j)=max(f(i-1,j),f(i-1,j-w[i])+v[i] ...

  8. 【泛化物品】【HDU1712】【ACboy needs your help】

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. hdu1712 分组背包 ACboy needs your help

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  10. 简单分组背包ACboy needs your help(hdu1712)

    题意:有n个任务,完成期限是m天,a[i][j]代表第i个任务用j天完成可以获得的利益,问在这m天里面可以获得的最大利益,每次只能做一个任务,即多个任务不能同时做; 分析;用dp[i][j]代表在做第 ...

随机推荐

  1. Michael Kors - Wikipedia, the free encyclopedia

    Michael Kors - Wikipedia, the free encyclopedia Michael Kors From Wikipedia, the free encyclopedia   ...

  2. &&与||的用法总结

    a() && b() :如果执行a()后返回true,则执行b()并返回b的值:如果执行a()后返回false,则整个表达式返回a()的值,b()不执行: a() || b() :如果 ...

  3. 【MongoDB数据库】MongoDB 命令入门初探

    MongoDB是一款NoSql数据库,使用了"面向集合"(Collection-Oriented)原理,意思是数据被分组存储在数据集中,被称为一个集合(Collection).每一 ...

  4. 无废话ubuntu 13.4w文件共享配置

    目标:实现windows和linux混合组成的操作 系统中可以共享文件,并可以通过机器名互相访问 安装文件共享服务 0.更改本机主机名,修改 /etc/hostname文件和/etc/hosts文件中 ...

  5. Git上手指南

    (写在最前:这篇随笔是我在学习git时参考资料+实践出来的,其中一些问题是在实践中遇到的,希望对大家有帮助,还有很多不完整的地方.如果有什么错误的地方欢迎随时向我提出来) 在Git教程之前,我们先来了 ...

  6. ASPxGridView-为每行添加序号

    添加一个新的非绑定列,使用CustomColumnDisplayText事件来分配序号给该列 <dx:GridViewDataTextColumn Caption="序号" ...

  7. English - 英语学习小笔记

    1.It is...to do sth:做某事是.... 解析:It 是形式主语,后面一半接形容词做表语,to do sth是不定式短语作真正主语. 2.make do和make doing是两种表达 ...

  8. 表A中有两个表示时间的字段A,B;如果B的值大于A的值,则把B的值更新为A的值

    sql语句:表A中有两个表示时间的字段A,B:如果B的值大于A的值,则把B的值更新为A的值 update 表名 set B=A where B>A

  9. codeforces 632D. Longest Subsequence 筛法

    题目链接 记录小于等于m的数出现的次数, 然后从后往前筛, 具体看代码. #include <iostream> #include <vector> #include < ...

  10. PCRE兼容正则表达式函数

    1.preg_grep()函数 函数语法: array preg_grep ( string pattern, array input ) 函数功能: 使用数组input中的元素一一匹配表达式patt ...