Brush the fence, only horizontal and vertical, vertical is certainly the most is N, another so every time the bottom of the brush off, brush off, continue to brush off the above, each time the remaining press sideways or vertical brush
It's a split treatment.
#include <cstdio> #include <cstring> #include <algorithm> #include <vector>using namespace std; #define INF 0x7f7f7f7fint a[6000];int n;int ans;int len;int dfs (int l,int r) { int i,mina = INF; int sum = 0; for (i = L;i <= r; i++) mina = min (mina,a[i]); for (i = L;i <= r; i++) a[i]-= mina; sum = mina; int ll = l; for (i = L;i <= r; i++) { if (a[i] = = 0) { sum + = DFS (ll,i-1); ll = i+1; } } if (ll <= r) sum + = DFS (ll,r); return min (sum,r-l+1);} int main () { int n,i; while (~SCANF ("%d", &n)) {for (i=0;i<n;i++) scanf ("%d", &a[i]); printf ("%d\n", DFS (0,n-1)); } return 0;}
Codeforces 448C Painting Fence