HDU1712-ACboy needs your help
描述:
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的更多相关文章
- P1757 通天之分组背包 / hdu1712 ACboy needs your help (分组背包入门)
P1757 通天之分组背包 hdu1712 ACboy needs your help hdu1712题意:A[i][j]表示用j天学习第i个课程能够得到A[i][j]的收益,求m天内获得的收益最大值 ...
- HDU1712:ACboy needs your help(分组背包模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem Description ACboy has N courses this term, an ...
- hdu1712 ACboy needs your help 分组背包
最基础的分组背包~ #include <iostream> #include <cstdio> #include <cstdlib> #include <cs ...
- 分组背包----HDU1712 ACboy needs your help
很简单的一道分组背包入门问题.不多解释了. #include <iostream> #include <cstdio> #include <cstring> usi ...
- [hdu1712]ACboy needs your help分组背包
题意:一共$m$天,$n$门课程,每门课程花费$i$天得到$j$的价值,求最后获得的最大价值 解题关键:分组背包练习,注意循环的顺序不能颠倒 伪代码: $for$ 所有的组$k$ $for{\rm ...
- HDU1712 ACboy needs your help(分组背包)
每种课程学习不同天数可以获得不同价值,这可以看成一个组,那么题目就是分组背包的模板题了. 1 #include<cstdio> 2 #include<cstring> 3 #i ...
- 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] ...
- 【泛化物品】【HDU1712】【ACboy needs your help】
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu1712 分组背包 ACboy needs your help
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 简单分组背包ACboy needs your help(hdu1712)
题意:有n个任务,完成期限是m天,a[i][j]代表第i个任务用j天完成可以获得的利益,问在这m天里面可以获得的最大利益,每次只能做一个任务,即多个任务不能同时做; 分析;用dp[i][j]代表在做第 ...
随机推荐
- javascript编辑器预览模式解密
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Error pulling origin: error: The following untracked working tree files would be overwritten by...
git在pull时,出现这样的错误的时候,可能非常多人进进行stash.相关stash的请看:Error pulling origin: error: Your local changes to th ...
- 遍历文件夹及其子文件夹下的.pdf文件,并解压文件夹下所有的压缩包
List<PDFPATH> pdfpath = new List<PDFPATH>(); List<string> ziplist = new List<st ...
- 使用weight属性实现视图的居中显示
有的时候我们开发一个产品的时候需要让其中某个控件的宽度或高度占据其父容器的宽度或高度的一半显示,这个时候由于设备尺寸的限制,做到在每个设备上都具有同样的效果的话,我们就需要用到weightSum属性和 ...
- asp.net textbox keyup事件触发后台的textchange事件
textbox文本框text_change事件,失去焦点才会执行. 通过keyup事件,js控制失去焦点. <asp:TextBox runat="server" ID=&q ...
- SQL Server数据库连接字符串整理
1.sql验证方式的 Data Source=数据源;Initial Catalog= 数据库名;UserId=sql登录账号;Password=密码; Eg: Data Source=.;Initi ...
- javascript笔记—面向对象
什么是对象: 对象是一个整体,对外提供一些操作. 什么是面向对象: 使用对象时,只关注对象提供的功能,不关注其内部细节,例如jquery 面向对象是一种通用思想,并非只有编程中能用,任何事情都可以用. ...
- 本地搭建php环境
AppServ 是 PHP 网页架站工具组合包,所包含的软件有:Apache[.Apache Monitor.PHP.MySQL.phpMyAdmin等,如果您的本地机器没有安装过php.mysql等 ...
- URL伪静态设置 (apache2.4)
` ` 1.修改apche主配置文件 主要是 #LoadModule rewrite_module modules/mod_rewrite.so 改为 LoadModule rewrite_modul ...
- hdu 1823 Luck and Love 二维线段树
题目链接 很裸的题, 唯一需要注意的就是询问时给出的区间并不是l<r, 需要判断然后交换一下, WA了好多发... #include<bits/stdc++.h> using nam ...