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

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 }

 

转载于:https://www.cnblogs.com/weiyuan/p/5789954.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值