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. openstack私有云布署实践【11.1 计算nova - compute节点配置(科兴环境)】

    这里我只使用kxcompute1节点配置为示例,其它节点的配置基本是一样的,只是声明的管理IP不同而已   计算节点 # yum install openstack-nova-compute sysf ...

  2. EasyUI DataGrid 添加 Footer

    做后台管理界面时,EasyUI 的 DataGrid 经常会被用到,有时候一些总的统计数据不合适放在数据表格里,需要单独显示,这时候就可以放在Footer中显示而不必另外布局. 该怎么给 DataGr ...

  3. 生成 Qt 文档

    个人总结    从命令行进入Qt安装目录     设置环境变量     set path=D:/mingw32/bin;D:/Qt/5.0.0/qtbase/bin;D:/icu/bin;D:/icu ...

  4. 《Intel汇编第5版》 条件汇编伪指令

    一.条件汇编伪指令和宏使用可以使汇编程序更加灵活 二.通过伪指令来检查函数的参数是否为空,如果为空则输出警告信息 INCLUDE Irvine32.inc includelib Irvine32.li ...

  5. Java笔试题目-my

    1.BuildString  和 BefferedString  默认初始容量为 16 ,  超出就是 16*2 + 2;   可以看源代码得知; 2.ArrayList 默认的构造方法是一个空的Ob ...

  6. jdk7 HashSet和HashMap源码分析

    先来看看HashMap的一些成员变量以及他们的含义 /** * The default initial capacity - MUST be a power of two. */ static fin ...

  7. 自己通过反射写的一个属性copy类

    package com.xxx.beancopier; import java.lang.annotation.Documented; import java.lang.annotation.Elem ...

  8. 【转】CSS

    css概念 http://www.cnblogs.com/moveofgod/archive/2012/09/18/2691101.html css八大功能 http://developer.51ct ...

  9. x264中的帧类型、条带类型、数据分区(data partition)

    1 条带类型(slice type) x264的条带有三种基本类型分别为:I(主要用于帧内图像编码).P(用于帧间前向参考预测图像编码).B(用于帧间双向参考预测图像编码).SI与SP(切换码流时用) ...

  10. 在java中json的使用案例

    import java.text.ParseException; import org.json.JSONArray; import org.json.JSONObject; public class ...