Simple dp
Requirement: calculate the maximum value from dp [0] [5] to dp [t] []
Similar to the data Tower, only 1 ~ The number 9 has three directions: 0 and 10. There are only two options.
We can regard all the time periods and pies as a matrix. Time is the number of rows, and the number of columns is the number triangle. We can find a path from the bottom layer, make the sum on the path largest.
Dp [I] [j] indicates the maximum value of the j position at the I moment.
Do not understand why for t-> 0 for 1-> 10 and then get dp [0] [5]
First, we need to push forward from the last second, just as the number tower starts to calculate from the bottom layer.
Then calculate the maximum value that can be obtained for each position. because you do not know whether the next position in these locations is the end point, every position must be calculated from 0-> t.
# Include <iostream> # include <cmath> # include <cstdio> # include <vector> # include <cstring> # include <algorithm> using namespace std; int dp [100005] [15], n, t; int max3 (int a, int B, int c) {int m = 0; if (a> m) m = a; if (B> m) m = B; if (c> m) m = c; return m;} int main () {int I, j,, b; while (scanf ("% d", & n) {memset (dp, 0, sizeof dp); t = 0; while (n --) {scanf ("% d", & a, & B); dp [B] [a] ++; t = max (t, B );} for (I = t; I> 0; I --) // push forward from the last second {for (j = 0; j <= 10; j ++) // calculate the maximum value with j as the end point every second {if (j = 0) // j = 0 or 10 only select {dp [I-1] [j] + = max (dp [I] [j], dp [I] [j + 1]);} else if (j = 10) {dp [I-1] [j] + = max (dp [I] [j], dp [I] [J-1]);} else {dp [I-1] [j] + = max3 (dp [I] [j], dp [I] [J-1], dp [I] [j + 1]) ;}} printf ("% d \ n", dp [0] [5]);} return 0 ;}