题目大意就是给出 N门课, 然后给出每门课花m天的收益,问在天数一定的情况下最大收益
ans[x][r] 表示前x节课在r天的情况下的最佳情况
for ( int i= 0, r)
ans[x][r] = max( ans[x][r] , ans[x+1][r-i] + cl[x][i])
用了for和dfs两种方法写
题目:
ACboy needs your help
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3368 Accepted Submission(s): 1742
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
Source
Recommend
lcy
代码:
1 //Sat Jan 11 18:55:27 2014 2 //Author:Minshik 3 #include <iostream> 4 #include <algorithm> 5 #include <cstring> 6 #include <cstdio> 7 #include <string> 8 #include <vector> 9 #include <set> 10 #include <stack> 11 #include <queue> 12 #include <deque> 13 #include <memory.h> 14 #include <cctype> 15 #include <cmath> 16 #include <list> 17 using namespace std; 18 19 int TIAN,KE; 20 int cl[105][105]; 21 int ans[105][110]; 22 23 #define REP(i,N) for(int i=0;i<N;i++) 24 25 int dfs(int x,int r) 26 { 27 if(ans[x][r]) return ans[x][r]; 28 29 if(x==KE)return 0; 30 31 int &d = ans[x][r]; 32 33 d = dfs(x+1,r); 34 35 for(int i=1;i<=r;i++) 36 { 37 d = max(d,dfs(x+1,r-i)+cl[x][i-1]); 38 } 39 return d; 40 41 } 42 int main() 43 { 44 45 while(cin>>KE>>TIAN && KE+TIAN!=0) 46 { 47 REP(i,KE)REP(j,TIAN) 48 cin>>cl[i][j]; 49 50 51 52 for(int i=1;i<=TIAN;i++) 53 { 54 for(int j=1;j<=KE;j++) 55 { 56 ans[j][i] = ans[j-1][i]; 57 for(int k=1;k<=i;k++) 58 { 59 ans[j][i] = max (ans[j][i], ans[j-1][i-k] +cl[j-1][k-1] ); 60 } 61 62 } 63 } 64 65 66 67 68 cout<<ans[KE][TIAN]<<endl; 69 70 71 } 72 73 74 return 0; 75 }