Give n courses, review the n courses in m days, and then give A matrix A of n * m, A [I] [j] indicates the benefits of reviewing the I course on day j. Q: The maximum benefit of reviewing different classes in m days.
Solution: This question is not difficult, but the data is very confusing. At first I thought that one course can be reviewed twice, then, we processed n * m items as n * m items in a 01 backpack, and submitted a gorgeous return Wa. After reading the questions again, You must select a different course. This is the grouping of backpacks, so that you can only choose a review for this course.
State transition equation: dp [k] = min (dp [k], dp [k-j] + pro [I] [j]) (I indicates the I-th course, k indicates the number of days, and pro [I [j indicates the number of j-days for the I-th Department ). complexity (O (n * m ). note that the enumeration sequence of the number of days must be large to small, so as to ensure that the State required for each calculation is the previous one, in order to ensure the efficiency.
Test data:
2 3
4 4 1
3 2 1
1 2
4
Code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Deprecision MAX 110
Int n, m, ans, dp [MAX];
Int pro [MAX] [MAX];
Int main ()
{
Int I, j, k, t;
While (scanf ("% d", & n, & m), n + m ){
For (I = 1; I <= n; ++ I)
For (j = 1; j <= m; ++ j)
Scanf ("% d", & pro [I] [j]);
Memset (dp, 0, sizeof (dp ));
For (I = 1; I <= n; ++ I)
For (k = m; k> = 1; -- k)
For (j = k; j> = 1; -- j)
If (dp [k-j] + pro [I] [j]> dp [k])
Dp [k] = dp [k-j] + pro [I] [j];
For (ans = I = 0; I <= m; ++ I)
If (dp [I]> ans) ans = dp [I];
Printf ("% d \ n", ans );
}
}
From ZeroClock