码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 3666 Making the Grade(dp+离散化)

时间:2016-08-20 11:21:14      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

Description

A straight dirt road connects two fields on FJ‘s farm, but it changes elevation more than FJ would 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 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 each 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 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

A B 1| + | A B 2| + ... + | 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

题目链接:http://poj.org/problem?id=3666

推荐题解链接:http://www.hankcs.com/program/cpp/poj-3666-making-the-grade.html

**********************************************

题意:给定一个正整数序列a[1...n],要求你改变每一个数变成b[1...n],使得改变后的序列非严格单调,改变的代价为abs(a[1]-b[1])+...+abs(a[n]-b[n]),求代价最小值。

分析:dp+离散化。

 

显然b[i]必定为a[1...n]中的某个值,且由于a过大,所以离散化。我们将每个数离散化 然后排序。设dp[i][j]为第i个数改变为b[j]时代价最小值,

则dp[i][j]=min(dp[i-1][k])+abs(a[i]-b[j]),然后 b数组表示的就是 离散过的 那些数据。

 

AC代码:

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<time.h>
 7 #include<stack>
 8 using namespace std;
 9 #define N 12000
10 #define INF 0x3f3f3f3f
11 
12 int b[N],a[N],dp[N];
13 
14 int main()
15 {
16     int n,i,j;
17 
18     while(scanf("%d", &n) != EOF)
19     {
20         memset(dp,0,sizeof(dp));
21         memset(a,0,sizeof(a));
22         memset(b,0,sizeof(b));
23 
24        for(i=0;i<n;i++)
25        {
26            scanf("%d", &a[i]);
27            b[i]=a[i];
28        }
29 
30        sort(b,b+n);
31 
32        for(i=0;i<n;i++)
33         dp[i]=abs(a[0]-b[i]);
34 
35        for(i=0;i<n;i++)
36        {
37            int mi=INF;
38            for(j=0;j<n;j++)
39            {
40                mi=min(mi,dp[j]);
41                dp[j]=mi+abs(a[i]-b[j]);
42            }
43        }
44 
45         int minn=INF;
46         for(i=0;i<n;i++)
47             minn=min(dp[i],minn);
48 
49         printf("%d\n",minn);
50     }
51     return 0;
52 }

 

POJ - 3666 Making the Grade(dp+离散化)

标签:

原文地址:http://www.cnblogs.com/weiyuan/p/5789954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!