Beans
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total Submission (s): 3521 Accepted Submission (s): 1681
Problem Description
Bean-eating is a interesting game, everyone owns an m*n matrix, which are filled with different qualities beans. Meantime, there is the only one bean in any 1*1 grid. Now your want to eat the beans and collect the qualities, but everyone must obey by the following rules:if you eat the BEA n at the coordinate (x, y), you can ' t eat the beans anyway at the coordinates listed (if exiting): (x, Y-1), (x, y+1), and The both rows whose Abscissas are x-1 and x+1.
Now, how much qualities can do you eat and then get? Input There is a few cases. In each case, there is the integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of beans isn ' t beyond, and 1<=m*n<=200000. Output for each case, you just output the MAX qualities you can eat and then get. Sample Input
4 611 0 7 5 13 978 4 81 6 22 41 40 9 34 16 1011 22 0 33 39 6
Sample Output
242
Source multi-university Training Contest 4-host by HDU
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2845
Main topic: Select some number in a matrix, requirements and maximum, if the number of (x, Y) position is selected, then (×, y+1), (x,y-1) position is not selectable, x+1 and x-1 lines are not selectable
Topic Analysis: The topic gave the scope of the m*n, is not let you open two-dimensional open happy, but do not affect, one-dimensional so do, first for each line DP, find out the current line can get the maximum value
TMP[J] = max (tmp[j-1],a[i + j-1] + tmp[j-2]) first indicates that the number is not selected in line I, the second one is selected, whichever is maximum, then the last Tmp[m] is the largest of the current row
And then because the next two lines can't be taken at the same time, I'll do the DP again.
Dp[i] = max (dp[i-1], dp[i-2] + row[i]), the first indicates that I do not select line I, the second is the choice of row I, take maximum, then the last dp[cnt-1] is the answer
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const MAX = 2 * 1e5 + 5;i NT Row[max], A[max], Dp[max], Tmp[max];int main () { int n, m; while (scanf ("%d%d", &n, &m)! = EOF) { memset (tmp, 0, sizeof (TMP)); memset (DP, 0, sizeof (DP)); for (int i = 1; I <= m * n; i++) scanf ("%d", &a[i]); int cnt = 1; for (int i = 1; I <= m * n; i + = m) {for (int j = 2; J <= M; j + +) { tmp[1] = a[i]; TMP[J] = max (tmp[j-1], a[i + j-1] + tmp[j-2]); } Row[cnt + +] = Tmp[m]; } DP[1] = row[1]; for (int i = 2; i < cnt; i++) dp[i] = max (dp[i-1], dp[i-2] + row[i]); printf ("%d\n", Dp[cnt-1]);} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2845 Beans (two-time linear DP)