Question: codeforces 448 cpainting fence
Question: There are n 1 * A [I] boards standing up and turning them into a barrier with a width of 1 to a [I]. Now paint the barrier, the width of the brush is 1. Each brush can be any longer. How many brushes do you need at least?
Analysis: The question seems to have no clue. It is actually very easy to analyze it carefully.
First of all, if we brush a board each time, that is, a vertical line, then we need to finish the brush n times, it can be seen that this is the maximum value of an ans. In the worst case, I can use N at most.
Second, if we choose a horizontal brush, and the shortest of N boards is Min, we can spend min to brush them to the height of a [I]-min, then the rest of the fence becomes the beginning. We can continue to click the above method when selecting X which is not 0. We can see that it is a recursive call.
Pay special attention to the preceding conditions, that is, to brush x boards and use X at most. If the number is greater than X at a time, take X, which makes it easy.
In fact, it can be attributed to: in fact, the initial thought can be attributed to the priority of horizontal brush, followed by vertical brush (if the cost of vertical brush is smaller), pay attention to be careful.
Code:
# Include <cstdio >#include <iostream >#include <string >#include <cstring >#include <algorithm> # include <cmath> using namespace STD; const int n = 5500; int A [n]; int TMP = 0, ANS = 0; void solve (int s, int t) {int MA =-1, MI = 0x3f3f3f; for (INT I = s; I <t; I ++) {if (a [I]> Ma) Ma = A [I]; if (A [I] <mi) MI = A [I];} If (MA = mi) {TMP + = min (MI, t-s); return ;} for (INT I = s; I <t; I ++) A [I]-= mi; MI = min (MI, t-s); TMP + = mi; for (INT I = s; I <t; I ++) {if (a [I]> 0) {for (Int J = I; j <t; j ++) /// enumeration continuous not 0 Segment {if (a [J] = 0 | j = (t-1) & A [J]> 0) {If (j = (t-1) & A [J]> 0) J ++; int KK = TMP; solve (I, j ); if (TMP-kk> (J-I) {// determine if the obtained value is larger than that of a row directly, take a smaller TMP = KK + (J-I);} I = J; break ;}}} int main () {int N; while (~ Scanf ("% d", & N) {for (INT I = 0; I <n; I ++) scanf ("% d ", & A [I]); TMP = 0; ans = 0; solve (0, n); printf ("% d \ n", min (n, TMP ));} return 0 ;}
Codeforces 448 cpainting fence