Poj 3666 Making the Grade (Dynamic Planning)

Source: Internet
Author: User

Poj 3666 Making the Grade (Dynamic Planning)

 

Making the Grade
Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:4647   Accepted:2202

 

Description

A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ wowould like. his cows do not mind climbing up or down a single slope, but they are not fond of an alternating succession of hills and valleys. FJ wowould like to add and remove dirt from the road so that it becomes one monotonic slope (either sloping up or down ).

You are givenNIntegersA1 ,...,AN(1 ≤N≤ 2,000) describing the elevation (0 ≤Ai≤ 1,000,000,000) at eachNEqually-spaced positions along the road, starting at the first field and ending at the other. FJ wocould like to adjust these elevations to a new sequenceB1 ,....,BNThat is either nonincreasing or nondecreasing. Since it costs the same amount of money to add or remove dirt at any position along the road, the total cost of modifying the road is

| A1- B1 | + | A2- B2 | +... + | AN- BN|

Please compute the minimum cost of grading his road so it becomes a continuous slope. FJ happily informs you that signed 32-bit integers can certainly be used to compute the answer.

Input

* Line 1: A single integer:N
* Lines 2 ..N+ 1: LineI+ 1 contains a single integer elevation:Ai

Output

* Line 1: A single integer that is the minimum cost for FJ to grade his dirt road so it becomes nonincreasing or nondecreasing in elevation.

Sample Input

71324539

Sample Output

3

One idea is: for a [I], enumerate any height as the maximum height, take the legal optimal solution of the previous I-1.

The data range is large: 10 ^ 9, but n is only 2000 in size. It can be discretization and the height can be replaced by coordinates.

A [I] stores the original array, and B [j] stores the ascending array after sorting.

Dp [I] [j] = min (dp [I-1] [0 .. j]) + abs (a [I]-B [j]); (change the height of the number of I to B [j], the minimum cost at this time .)

 

 

#include
 
  #include
  
   #include
   
    #include
    
     #include#include
     
      #include
      
       using namespace std;#define ll __int64#define mem(a,t) memset(a,t,sizeof(a))#define N 2005const int M=305;const int inf=0x7fffffff;int a[N],b[N];int dp[N];void solve(int n){ int i,j,tmp; sort(b,b+n); for(i=0;i
       
        

 

 

 

 

 

 

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.