[Usaco2008feb_gold] Pavement Trimming

Source: Internet
Author: User

FJ is going to take a good look at some rugged dirt road in the farm. According to the demands of the cows, the repaired
The height of the pavement should be monotonically increasing or monotonically decreasing, that is to say, height rise and height descent cannot
At the same time appeared in the repaired road.

The whole road is divided into n segments, n integers a_1, ..., A_n (1 <= N <= 2,000) are described in turn
The height of each section of the road (0 <= a_i <= 1,000,000,000). FJ wants to find an exact n
The element does not rise or fall in sequence b_1, ..., b_n, as the height of each section of the road being repaired.
The total cost of road repairs can be expressed by the same amount of overhead or digging down one unit per section:

| a_1-b_1| + | a_2-b_2| + ... + | a_n-b_n|

Would you please calculate the minimum cost of FJ in this project? FJ assures you that this expense
No more than 2^31-1.

Initial discretization;

No descending sequence:

Setting F[i][j] means the minimum cost of repairing to block I, when he is repaired to the height of the section J or below;

F[i][j]=min (F[i][j],f[i-1][j]+abs (a[i]-q[j));

Then the analogy does not rise;

[Usaco2008feb_gold] Pavement Trimming

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.