Question connection
- Question:
Input the height of N wooden bars. The width of each wooden bar is 1. Now there is a brush with a length and a width of 1. You can choose to brush it horizontally or vertically each time. You cannot leave the wooden stripe every time you paint it. You need to paint it several times at least. (You can go through the place where you have already been swiped)
N (1? ≤?N? ≤? 5000 ),1? ≤?AI(Height )? ≤? 109
- Analysis:
Analyze the relationship between horizontal and vertical: if there is already an operation set, S is the answer, then the operation sequence in the set can be changed, that is, the order of horizontal and vertical colors can be changed (because the colored wooden bars can be passed), so you may wish to color them horizontally first. For the current [L, R] range, the answer will not exceed R-l + 1. Set the shortest wood stripe to H1. You can brush H1 times horizontally, then divide the interval into two intervals to solve the problem, and finally obtain the length of Min.
Const int maxn = 51000; int H [maxn], n; int fun (int l, int R, int sub) {If (L> r) return 0; int min = inf, ID; Fe (I, L, R) if (H [I] <min) {min = H [I]; id = I ;} return min (R-l + 1, fun (L, ID-1, min) + fun (ID + 1, R, min) + min-sub );} int main () {While (~ RI (N) {Fe (I, 1, n) RI (H [I]); cout <fun (1, n, 0) <Endl ;} return 0 ;}