Codeforces 448C C. Painting Fence (Division +DP)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.