HDU1712

传送门: 点击打开链接

ACboy needs your help

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


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 接下来一个n行m列的矩阵,n代表学科数,m代表天数,每个矩阵的位置代表i学科学习j天的成果,求m天成果最大为多少。

解题思路:分组背包,每一门学科的不同天数的成果互斥(相当于你学了1天就不能学2天了,总之就是只能选一个天数),把每一门学科分成一组。

AC代码如下:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<stdlib.h>
using namespace std;

const int maxn=100+10;

int dp[maxn],data[maxn][maxn];

int max(int a,int b)
{
	return a>b?a:b;
}

int main()
{
	int n,m,i,j,k;
	while(cin>>n>>m && n+m)
	{
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
			cin>>data[i][j];
		for(k=1;k<=n;k++)
			for(i=m;i>=1;i--)
				for(j=1;j<=i;j++)
					dp[i]=max(dp[i],dp[i-j]+data[k][j]);
		cout<<dp[m]<<endl;
	}
	return 0;
}

全部评论

相关推荐

06-19 13:40
武汉大学 Java
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
风中翠竹:真的真的真的没有kpi。。。面试官是没有任何kpi的,捞是真的想试试看这个行不行,碰碰运气,或者是面试官比较闲现在,没事捞个人看看。kpi算HR那边,但是只有你入职了,kpi才作数,面试是没有的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务