传送门: 点击打开链接
Total Submission(s): 7973 Accepted Submission(s): 4392
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.
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;
}