Article Title, article title Daquan

Source: Internet
Author: User

Article Title, article title Daquan

Making the Grade
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submissions: 4732 Accepted: 2244
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 given N integers A1 ,... , AN (1 ≤ N ≤ 2,000) describing the elevation (0 ≤ Ai ≤ 1,000,000,000) at each of N equally-spaced positions along the road, starting at the first field and ending at the other. FJ wowould like to adjust these elevations to a new sequence B1 ,.... , BN that 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: Line I + 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

7
1
3
2
4
5
3
9
Sample Output

3
Source

/*
Change some data so that the data is not strictly incrementing (1 2 3 3 4) (the general trend is increasing and the number can be the same)

Idea: dp [I] [j] indicates the minimum consumption value of the first I number to meet the question and the number of I is smaller than the j number.
So dp [I] [j] = min (dp [I-1] [k] + a [I]-B [j]) (B [j]) representing the j-smaller number after sorting
Where 1 <= k <= j; then, it is found that the array can be scrolled.
*/

Includemo-demo-demo-demo-demo-demo-demo-deinclude Includeinclude

Typedef _ int64 ll;

Define L (x) (x <1) define R (x) (x <1 | 1) define MID (x, y) (x + y)> 1)

Using namespace std;

Define INF 0x3 f3f3fdefine N 2005

Ll dp [2] [N];

Int a [N], B [N];
Int n;

Int main ()
{
Int I, j;
While (~ Scanf ("% d", & n ))
{
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & a [I]);
B [I] = a [I];
}
Sort (B + 1, B + n + 1 );
For (I = 1; I <= n; I ++)
Dp [0] [I] = fabs (a [1]-B [I]);
Int now = 0;
For (I = 2; I <= n; I ++)
{
_ Int64 temp = dp [now] [1]; // temp record dp [I-1] [1 ~ J] minimum value
For (j = 1; j <= n; j ++)
{
Temp = min (temp, dp [now] [j]);
Dp [now ^ 1] [j] = temp + abs (a [I]-B [j]);
}
Now = now ^ 1;
}
_ Int64 ans = dp [now] [1];

    for(i=2;i<=n;i++)        if(dp[now][i]<ans) ans=dp[now][i];    printf("%I64d\n",ans);}

Return 0;
}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.