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. CodeForces 707D Persistent Bookcase

    $dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. ...

  2. request获取ip

    public static String getIp(HttpServletRequest request) { String ip = request.getHeader("x-forwa ...

  3. Unity人工智能学习—确定性AI算法之追踪算法一

    转自http://blog.csdn.net/zhangxiao13627093203/article/details/47451063 尽管随机运动可能完全不可预知,它还是相当无趣的,因为它完全是以 ...

  4. fedora22 mysql安装

    fedora19以后好像取消了对mysql的支持,看其他人好像说是用的mariadb的.centos里用yum安装的方式,放到fedora中不能用,所以找了很多资料,尝试了一种可行的办法. 在Fedo ...

  5. 聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut

    原文请戳:http://blog.csdn.net/abcjennifer/article/details/8170687 聚类算法是ML中一个重要分支,一般采用unsupervised learni ...

  6. Windows 多用户远程访问 Ubuntu 14.04桌面

    使用X2Go实现多用户远程访问 Ubuntu 14.04桌面:VNC也可以,但是每次连接VNC就回新创建一个Seession,想要在下次远程登录的时候返回上次活动,需要记住开启的线程,这种繁琐的操作不 ...

  7. 工具类 util.Date 日期类

    /** * @description format the time * @author xf.radish * @param {String} format The format your want ...

  8. SharpZipLib.dll 压缩文件,可以应用于MVC, webform. C# windows application 等等地方

    Nuget 安装:Install-Package ICSharpCode.SharpZipLib.dll private void WriteZipFile(string[] filesToZip, ...

  9. C# DataTable转实体通用方法

    public static class DataTableHelper { public static T GetEntity<T>(DataTable table) where T : ...

  10. 整理了一份React-Native学习指南

    原文:  http://www.w3ctech.com/topic/909 自己在学习React-Native过程中整理的一份学习指南,包含 教程.开源app和资源网站等,还在不断更新中.欢迎pull ...