Question:
There are multiple pieces of wood to be filled, you can apply a row at a time, you can also apply a column at a time, of course, when you paint a line of wood piece with insufficient length in the middle, fill in to this interruption
This topic can be easily solved by using DFS, although it is still very difficult to understand.
1 # include <iostream> 2 3 using namespace STD; 4 5 # define n 5010 6 int A [n], n; 7 8 int min (int c, int D) 9 {10 return C <D? C: D; 11} 12 int dfs (int c, int D, int X) // get the part from C to D, perform at least several operations 13 {14 if (C> d) return 0 after the x length has been fully filled in; // when the C ratio is D, do not fill it out, so return 015 int T = C, K = A [c]; 16 for (INT I = C; I <= D; I ++) 17 if (a [I] <k) 18 k = A [I], t = I; // find the shortest D 19 Return min (DFS (C, T-1, a [T]) of the wood in C to D + A [T]-x + DFS (t + 1, D, a [T]), D-C + 1); // One is a horizontal query, one is vertical, get the minimum 20} 21 int main () 22 {23 while (CIN> N) {24 for (INT I = 0; I <N; I ++) CIN> A [I]; 25 cout <DFS (0, n-1, 0) <Endl; 26} 27 28 return 0; 29}