Topic Links:
Codeforces 448C
Main topic:
Give n poles, each pole has a length, each can brush a row or a column, ask the minimum number of brushes to brush the entire wall into yellow.
Topic Analysis:
- First we can think of, if the brush, in order to get the best solution, the current position of the brush must also be horizontal brush, and then for each case can be a vertical brush n to get the entire yellow wall.
- Therefore, we take the strategy of the Division of the dynamic planning, that is, for each state divided into two cases of discussion, if you want to brush the horizontal, the shortest to brush to the height of the shortest column may be compared to the vertical brush excellent solution, and then become a number of the same nature of the smaller walls, Then we can take the same strategy for the Division, know that the wall only a pillar, you can directly through a vertical brush to get the best solution, each decision to take the first horizontal brush and direct vertical brush two schemes of the smaller scheme.
AC Code:
#include <iostream>#include <cstdio>#include <algorithm>#define MAX 5007using namespace STD;typedef Long LongLL;intN LL A[max]; LL Dp[max][max];voidSolve (intLintR, LL h) {Dp[l][r] = r-l+1;if(L = = r)return; LL hh =1Ll<< -; for(inti = l; I <= R; i++) hh = min (hh, a[i]); LL ans = hh-h; for(inti = l; I <= R; i++) {if(A[i] = = hh)Continue;intJ for(j = i; J <= R; j + +) {if(j = r) Break;if(a[j+1] = = hh) Break; } Solve (I, J, HH); Ans + = dp[i][j]; i = j+1; } Dp[l][r] = min (Dp[l][r], ans);}intMain () { while( ~scanf("%d", &n)) { for(inti =1; I <= N; i++)scanf("%i64d", &a[i]); Solve (1N0);printf("%i64d\n", dp[1][n]); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 448C C. Painting Fence (Division +DP)