POJ 3666 Making the Grade

本文介绍了一种计算道路改造成本的方法,旨在将一条起伏不定的道路改造成连续的坡度,通过调整路面高度达到节省成本的目的。文章详细解释了使用动态规划算法进行计算的过程,并提供了两种实现方式,一种是直接使用二维数组存储中间结果,另一种则是利用滚动数组减少内存消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Making the Grade

Time Limit: 1000MS 

Memory Limit: 65536K

Total Submissions: 3122  Accepted: 1476

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

|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

思路:这题求的是不下降序列或者不上升序列。而网上的人说这道题数据水,只要求不下降的就可以了。首先定义状态dp[i][j]表示的是前 i 个数中,j 为最长不下降序列中的最大值时需要改变的花费。那么状态转移就是dp[i][j] = min(dp[i-1][1~j] + abs(a[i] - j)),意思是当前状态dp[i][j]是从上一个状态中d[i-1][1~j]中的最小值去加上abs(a[i]-j),也就是dp[i-1][1~j]中花费最少的,那么直接从这个状态转移过来,肯定也是最少的。然而 j 最大有1,000,000,000,不管是时间和空间都不允许操作。所以要离散化,既将原数列按升序排序(如果不离散化时,是从1循环到j,既从小到大的,升序排序的作用也是如此)。离散化的作用是将1,000,000,000的数变成只需要2000来表示,其中很多空余的数不需要考虑,因为它们并不存在于这个数列中。

31644k  266ms

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define SIZE 2005
 6 
 7 using namespace std;
 8 
 9 typedef __int64 Int;
10 
11 Int dp[SIZE][SIZE];
12 int array[SIZE];
13 int temp[SIZE];
14 int N;
15 
16 int Abs(int a)
17 {
18     return a > 0? a:(-a);
19 }
20 
21 int main()
22 {
23     while(~scanf("%d",&N))
24     {
25         for(int i=1; i<=N; i++)
26         {
27             scanf("%d",&array[i]);
28             temp[i] = array[i];
29         }
30         sort(temp+1,temp+1+N);
31         memset(dp,0,sizeof(dp));
32         for(int i=1; i<=N; i++)
33         {
34             Int mini = dp[i-1][1];
35             for(int j=1; j<=N; j++)
36             {
37                 mini = min(mini,dp[i-1][j]);
38                 dp[i][j] = Abs(array[i]-temp[j]) + mini;
39             }
40         }
41         Int ans = dp[N][1];
42         for(int i=1; i<=N; i++)
43             if(ans > dp[N][i])
44                 ans = dp[N][i];
45         printf("%I64d\n",ans);
46     }
47     return 0;
48 }

 考虑到每一层状态都是从上一个状态得来的,既只与上一个状态直接相关。所以可以用滚动数组。

180k   94ms

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define SIZE 2005
 6 
 7 using namespace std;
 8 
 9 typedef __int64 Int;
10 
11 Int dp[2][SIZE];
12 int array[SIZE];
13 int temp[SIZE];
14 int N;
15 
16 int Abs(int a)
17 {
18     return a > 0? a:(-a);
19 }
20 
21 int main()
22 {
23     while(~scanf("%d",&N))
24     {
25         for(int i=1; i<=N; i++)
26         {
27             scanf("%d",&array[i]);
28             temp[i] = array[i];
29         }
30         sort(temp+1,temp+1+N);
31         memset(dp,0,sizeof(dp));
32         for(int i=1; i<=N; i++)
33         {
34             Int mini = dp[(i+1)%2][1];
35             for(int j=1; j<=N; j++)
36             {
37                 mini = min(mini,dp[(i+1)%2][j]);
38                 dp[i%2][j] = Abs(array[i]-temp[j]) + mini;
39             }
40         }
41         Int ans = dp[N%2][1];
42         for(int i=1; i<=N; i++)
43             if(ans > dp[N%2][i])
44                 ans = dp[N%2][i];
45         printf("%I64d\n",ans);
46     }
47     return 0;
48 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值