POJ 3666 Making The Grade (sequence becomes the minimum cost of a non-descending/non-ascending array, DP)

Source: Internet
Author: User
Tags abs

Portal:

http://poj.org/problem?id=3666

Making the Grade
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9468 Accepted: 4406

Description

A straight dirt road connects the fields on FJ's farm, but it changes elevation more than FJ would like. His cows does not mind climbing to a single slope, but they is not fond of a alternating succession of hills and V Alleys. FJ would like to add and remove dirt from the road so that it becomes one monotonic slope (either sloping up or down).

You are given n integers A1, ..., an ( 1≤ N ≤2,000) describing the elevation (0≤ ai ≤1,000,000,000) at all of N equally-spaced positions along the road, starting at the first field and Ending at the other. FJ would like-to-adjust these elevations to a new sequence B1. ..., BN 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 |

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

Input

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

Output

* Line 1: A single integer, that's the minimum cost of FJ to grade he dirt road so it becomes nonincreasing or Nondecrea Sing in elevation.

Sample Input

71324539

Sample Output

3

Source

Usaco February Gold

Topic Meaning:

Given an integer sequence of length n, you can add a number 1 or minus 1 at a time, at least as many times as you want to turn it into a monotonically increasing or monotonically decreasing (not strictly).

Refer to the thoughts of netizens:https://www.cnblogs.com/Philip-Tell-Truth/p/4916026.html

is the farmer to repair a road, now requires that the road is either rising or falling, the total cost of ∑|a[i]-b[i]|, the lowest cost of road plan, (0≤ beta≤1,000,000,000), (1≤ N ≤2,000)

This question hundred percent is DP, why? We're trying to make the cost minimum, but we don't know how much cost is going to be minimal.

We can think of this, because the sequence is always ascending or descending, we can consider the rise in the case, assuming that the maximum value of the first few numbers is β, we have to consider changing the value from 0-β and then pushing it to nth sequence.

Obviously, this complexity is 0 (nβ^2), of course, this complexity is obviously an accident.

Now we want to optimize this thing, we can think, if we scan like before, then in fact we ignore a very important fact, that is the change to α (α<β), in fact, the α+1~β does not affect the alpha, So we can use a minimum value of the temporary variable stored in the minimum before α, with this update DP, so that the complexity of a sweep of the beta

Complexity into O (nβ);

But if that's the case, it's definitely tle, because Beta is just too big.

Then we need to use the idea of discretization, the beta projection into a finite area, since beta is the size of the function, then we can correspond to this: we only use the new sequence from small to large arrangement, and then only the subscript to find it, so we will be the solution of the space to the subscript.

Last state transfer equation: Dp[i-1][j]=abs (a[i]-b[j]) +min (Dp[i-1][j]); (with a scrolling array)

Specific practices:

first, it can be seen that all the numbers in the changed sequence must still be in the original sequence,
then sort the original sequence first .a B
1 3 2 4 5 3 9--1 2 3 3 4 5 9
then Dp[i][j] represents the minimum price to draw for the number of I, turning him into a b[j]
Dp[i][j] = dp[i-1] [0~j] + ABS (B[J]-a[i]) in this loop. Code
#include <stdio.h>#include<string.h>#include<memory>#include<iostream>#include<algorithm>using namespaceStd;typedefLong LongLL;#defineMax_v 2005#defineINF 0x7fffffffintN;intDp[max_v];intE[max_v];intB[max_v];intMain () {/*first, it can be seen that all the numbers in the changed sequence must still be in the original series, then the original sequence is sorted a B 1 3 2 4 5 3 9-1 2 3 3 4 5 9 then dp[i]   [j] denotes the number of I, turning him into b[j] the minimum price to draw dp[i][j] = dp[i-1] [0~j] + ABS (B[J]-a[i]) in this loop. */Cin>>N;  for(intI=0; i<n;i++) {cin>>E[i]; B[i]=E[i]; } sort (B,b+N);//array after ascending, compared to e    intans=INF; intT;  for(intI=0; i<n;i++) {T=INF;  for(intj=0; j<n;j++) {T=min (t,dp[j]); DP[J]=abs (B[j]-e[i]) +T; }    }     for(intI=0; i<n;i++) {ans=min (ans,dp[i]); } printf ("%d\n", ans); return 0;}

POJ 3666 Making The Grade (sequence becomes the minimum cost of a non-descending/non-ascending array, 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.