Codeforces round #256 (Div. 2) -- painting fence

Source: Internet
Author: User

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 ;}


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.