Question:
A tree branch has n segments, each of which has a score. You can select an interval that is not completely contained (can be intersection). The sum of all numbers in the interval can be obtained each time you select. Calculate the maximum score.
Problem solving process:
1. obviously, the default selection interval is from left to right. f [I] [J] indicates that the last selection interval is [I, j] optimal solution (I <= J ). Obviously f [I] [J] = max {f [p] [Q]} + sum [I] [J]. (P <I & q <j );
But the complexity is obviously too high, reaching O (N ^ 4 ).. The equation of this question is easy to think of, and it is rare that it is how to reduce complexity.
2. Considering that the condition of q <J in the equation is well satisfied, taking J as the stage, the value of the phase J-1 can satisfy q <j. Then we use an auxiliary array G [I] [J] To represent max {f [p] [Q]} (P <= I & q <= J ), maintenance G [I] [J] As long as each completion phase J, for I = 1-> j g [I] [J] = max {G [I-1] [J], f [I] [J], G [I] [J-1]};
In this way, the state transition equation becomes f [I] [J] = G [I-1] [J-1] + sum [I] [J]. the complexity is successfully reduced to O (N ^ 2 );
Summary: Some DP questions are generally easy to obtain the simplest state transition equation, but the complexity is often too high. Therefore, an auxiliary array needs to be maintained to reduce the complexity.
Jia's magic wand (vijos 1283)