Making the Grade POJ - 3666 (dp 离散化 变成 非严格单调递增 或递减)

本文介绍了一种用于计算使路径变为单调斜坡的最小成本的方法。该算法通过动态规划解决农夫约翰希望调整其农场中连接两个田野的道路的问题,确保道路上升或下降形成连续斜坡,以减少奶牛攀爬时遇到的起伏。

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

题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A_i,修理后是B_i,花费|A_i – B_i|,求最小花费。

思路:

为什么叫离散化,我不懂,但状态转移方程应该懂; 
 dp[i][j] 表示前i个数组成的非严格递增的序列的最大值为b[j] 的 最小花费;
状态转移方程; 
dp[i][j] = abs(a[i] - b[j]) + min(dp[i-1][k]) (k<=j);

若是严格单调递增的话,(k<j),因为这题上给出的n个数有重复,所以要求严格单掉递增的话,(k<j)也不行; 
要是给出的n个数没有重复的可以尝试一下这个思路(k<j);
非严格单调递增是大于等于前面的数;

若一个序列本身不是非严格单调递增的话,要想经过最优改变变成非严格单调递增的话,那么其中变得值一定是n个数中的数的值; 

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 2020
#define INF 0x3f3f3f3f
#include<stdlib.h>

int dp[Max][Max];
int a[Max],b[Max];
int n;
   
//为什么叫离散化,我不懂,但状态转移方程应该懂; 
//  dp[i][j] 表示前i个数组成的非严格递增的序列的最大值为b[j] 的 最小花费;
// 状态转移方程; 
//  dp[i][j] = abs(a[i] - b[j]) + min(dp[i-1][k]) (k<=j);
// 若是严格单调递增的话,(k<j),因为这题上给出的n个数有重复,所以要求严格单掉递增的话,(k<j)也不行; 
// 要是给出的n个数没有重复的可以尝试一下这个思路(k<j);
// 非严格单调递增是大于等于前面的数;
// 若一个序列本身不是非严格单调递增的话,要想经过最优改变变成非严格单调递增的话,那么其中变得值一定是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));      // 把给出的n个数从小到大排序; 
		memset(dp[0],0,sizeof(dp[0]));
		for(i = 1;i<=n;i++)
		{
			int Mi = INF;
			for(j = 1;j<=n;j++)
			{
				Mi = min(Mi,dp[i-1][j]);   // 找出前i-1个数变成小于等于b[j]结尾的非严格单调递增的最小花费; 
				dp[i][j] = abs(a[i] - b[j]) + Mi;
			}
		}
		int Mi = INF;
		for(i = 1;i<=n;i++)
			Mi = min(Mi,dp[n][i]);
		printf("%d\n",Mi);
	}
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值