B - ACboy needs your help

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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
题意:

ACboy有n门课可以参加,他有m天时间。一天只能参加一门课。一门课可以参加多天,参加的天数不同获得的经验不一样,并且一门课只能修习一次。但是要注意的是,同一门课不是参加的天数越多,经验就越高。输入的表示是,行数表示第几门课,列数表示上几天,该数就是第几门课参加几天获得的经验数。问m天能获得最多的经验数。

思路:分组背包,建立dp数组dp[i][j]表示修习前i种课程,修习了j天,所得的最大的经验值。

二维数组:

方法:状态转移方程为dp[i][j]=max{dp[i-1][j],dp[i-1][j-k]+xing[i][k]},意思是前i-1种课程,前j-k天获得的经验最大数是dp[i-1][j-k],后k天参加第i门课获得的经验是xing[i][k]。比较此时的dp[i][j]和dp[i-1][j-k]+xing[i][k]谁大。

AC代码:

 #include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring> using namespace std; int dp[][]={};
int xing[][]={}; int main()
{
// freopen("1.txt","r",stdin);
int n,m;
while(cin>>n>>m&&n!=&&m!=){
memset(dp,,sizeof(dp));
int i,j,k;
for(i=;i<=n;i++){//输入数据
for(j=;j<=m;j++)
cin>>xing[i][j];
}
for(i=;i<=n;i++){
for(j=;j<=m;j++)
for(k=;k<=j;k++){
if(dp[i][j]<=dp[i-][j-k]+xing[i][k])//进行比较
dp[i][j]=dp[i-][j-k]+xing[i][k];//状态转移
}
}
cout<<dp[n][m]<<endl;
}
return ;
}

一位数组:

方法:状态转移方程为dp[j]=max{dp[[j],dp[j-k]+xing[i][k]},意思是前j-k天获得的经验最大数是dp[j-k],后k天参加第i门课获得的经验是xing[i][k]。比较此时的dp[j]和dp[j-k]+xing[i][k]谁大。

AC代码:

 #include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring> using namespace std; int dp[]={};
int xing[][]={}; int main()
{
// freopen("1.txt","r",stdin);
int n,m;
while(cin>>n>>m&&n!=&&m!=){
memset(dp,,sizeof(dp));
int i,j,k;
for(i=;i<=n;i++){
for(j=;j<=m;j++)
cin>>xing[i][j];
}
for(i=;i<=n;i++){
for(j=m;j>=;j--)
for(k=;k<=j;k++){
if(dp[j]<=dp[j-k]+xing[i][k])
dp[j]=dp[j-k]+xing[i][k];
}
}
cout<<dp[m]<<endl;
}
return ;
}

B - ACboy needs your help(动态规划,分组背包)的更多相关文章

  1. Codeforces 946 D.Timetable-数据处理+动态规划(分组背包) 处理炸裂

    花了两个晚上来搞这道题. 第一个晚上想思路和写代码,第二个晚上调试. 然而还是菜,一直调不对,我的队友是Debug小能手呀(真的是无敌,哈哈,两个人一会就改好了) D. Timetable   tim ...

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

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

  3. HDU 1712 ACboy needs your help(分组背包)

    题意:给你n的课程组,每个课程组有m个课程,每个课程有一个完成时间与价值.问在m天内每组课程组最多选择一个,这样可以得到的最大价值是多少 题解:分组背包,其实就是每个课程组进行01背包,再在课程组内部 ...

  4. 【HDU1712】ACboy needs your help(分组背包)

    将背包九讲往后看了看,学习了一下分组背包.来做几道入门题,试试手. #include <iostream> #include <cstring> #include <cs ...

  5. BZOJ1296 [SCOI2009]粉刷匠 动态规划 分组背包

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1296 题意概括 有 N 条木板需要被粉刷. 每条木板被分为 M 个格子. 每个格子要被刷成红色或蓝 ...

  6. HDU - 1712 - ACboy needs your help 【分组背包】

    <题目链接> 题目大意:有n个课程,现在花M天来学习这些课程,学习每个课程花的天数所得到的价值不同,求M天怎么分配学习才能得到的价值最大.(这些课程得到的价值和所花天数的关系由矩阵给出) ...

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

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1712 解释看这里:http://www.cnblogs.com/zhangmingcheng/p/3940 ...

  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时间 ...

  10. 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 ...

随机推荐

  1. Linux软件安装管理 - CentOS

    ---恢复内容开始--- 1. 软件包管理简介 1.1 源码包 - 脚本安装包 1.2 二进制包(RPM包,系统默认包) - 依赖性 2. rpm命令管理(Redhat Package Manager ...

  2. Ubuntu下安装python相关数据处理

    01. Ubuntu下安装ipython sudo apt-get install ipython 02. Ubuntu下安装pip $ sudo apt-get install python-pip ...

  3. Chapter 2 Open Book——37

    I couldn't concentrate on Mike's chatter as we walked to Gym, and RE. didn't do much to hold my atte ...

  4. trove instance service 总结

    def create(self, req, body, tenant_id): # TODO(hub-cap): turn this into middleware LOG.info(_LI(&quo ...

  5. lucene 索引删除

    1.IndexWriter和IndexReader都有删除索引的方法:deleteDocuments(); 不建议使用IndexReader删除索引:使用IndexReader进行删除时,必须关闭所有 ...

  6. php,cgi,nginx关系

    nginx是服务器 什么是服务器? 例如:IIS,Apache,Nginx......主要是提供网上浏览网页的服务,应用层使用HTTP协议. CGI,FastCGI CGI全称是"公共网关接 ...

  7. asp.net 后台验证成功(弹出对话框)并跳转?不能实现

    原始 Context.Response.Write("<script></script>"); Response.Redirect(); 解决办法: Cli ...

  8. Scrapy框架使用—quotesbot 项目(学习记录一)

    一.Scrapy框架的安装及相关理论知识的学习可以参考:http://www.yiibai.com/scrapy/scrapy_environment.html 二.重点记录我学习使用scrapy框架 ...

  9. JAVA语法基础(课堂ppt问题总结)

    一:运行源代码EnumTest.java,分析运行结果. 代码如下: public class EnumTest { public static void main(String[] args) { ...

  10. BuildingAndRunningUAFServerUsingMaven

    https://github.com/eBay/UAF/wiki/BuildingAndRunningUAFServerUsingMaven(CLIonly) 实现uaf的demo,使用ebay的方案 ...