HDU1712 ACboy needs your help

本文介绍了一个关于课程学习计划的问题,需要在有限的时间内通过合理的安排获得最大的收益。使用动态规划的方法,通过构建二维数组dp[i][j]来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ACboy needs your help

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6340    Accepted Submission(s): 3498

Problem 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

 点击原题链接

题意:
n个课程,最多有m天可以用,每个课程花费不同的天数得到的收益不同,
第i个课程花费j天来学那么收益为a[i][j],问如何安排收益最大。
如第一测试数据: 
2  2
1  2  a[1][1] = 1;a[1][2] = 2;
1  3  a[2][1] = 1;a[2][2] = 3;
首先明确每个课程只会上一次。设dp[i][j]为前i个课程花费j天的最大收益,
枚举当前i课程的所有需要的天数k,并选择学还是不学对应的a[i][k]。
思路:
可以将每一门课看成一个分组,每门课不同天数的选择看成是分组的物品(显然只能有一个选择),
物品的费用即为花费的天数,物品的价值为题中给出的收获。该题中背包容量最大为M。
设dp[x]为前i组物品,在背包容量为x(即费用为x)时的最大价值。则将i从1到N进行过历遍后(第一重循环),dp[m]即为所求。


#include <stdio.h>
#include <string.h>
#define max(a,b)  a > b ? a : b 
int a[110],dp[1010];
int main()
{
	int n,m,i,j,k;
	//freopen("input.txt","r",stdin);
	while(scanf("%d%d",&n,&m) && (n || m))
	{
		memset(dp,0,sizeof(dp));
		for(i = 1;i <= n;i ++ )  //学i门课程时 
		{
			for(j = 1;j <= m;j ++ )
				scanf("%d",&a[j]);
			for(j = m;j > 0;j -- ) //当最多用j天去做时 
				for(k = 1;k <= j;k ++ ) //遍历用j天能k完成中得收益 
				{
					dp[j] = max(dp[j],dp[j - k] + a[k]);//取最大收益 
					//printf("dp[%d]=%d\n",j,dp[j]);
				}	 
		}
		printf("%d\n",dp[m]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值