【题解】poj3666 Making the Grade 线性DP

本文介绍了如何利用线性动态规划解决FJ农场道路平缓化问题,求解使得道路变为非递增或非递减斜坡的最小成本。通过状态转移方程F[i,j]=min{F[i-1,k]+|Ai-j|},离散化A并记录决策集合最小值,达到O(N^2)的时间复杂度。" 88801621,8183620,逆序存储的链表相加,"['数据结构', '链表操作', '算法', 'LeetCode']

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

题目链接

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 N N integers A 1 , ⋯   , A N ( 1 ≤ N ≤ 2 , 000 ) A_1,\cdots, A_N (1 ≤ N ≤ 2,000) A1,,AN(1N2,000) describing the elevation ( 0 ≤ A i ≤ 1 , 000 , 000 , 000 ) (0 ≤ Ai ≤ 1,000,000,000) (0Ai1,000,000,000) at each of N N 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 B 1 , ⋯   , B N B_1,\cdots, B_N 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 1 − B 1 ∣ + ∣ A 2 − B 2 ∣ + ⋯ + ∣ A N − B N ∣ | A_1 - B_1| + | A_ 2 - B_ 2| + \cdots + | A_N - B_N | A1B1+A2B2++ANBN
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 N N
Lines 2 ⋯ N + 1 2\cdots N+1 2N+1: Line i + 1 i+1 i+1 contains a single integer elevation: A i A_i 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


记最小值 S = ∑ i = 1 N ∣ A i − B i ∣ S=\sum_{i=1}^N|A_i-B_i| S=i=1NAiBi
本题最玄幻的地方:
在满足 S S S最小化的前提下,一定存在一种构造序列 B B B的方案,使得 B B B中的数值都在 A A A中出现过。
我们设 F [ i , j ] F[i,j] F[i,j] 表示完成前 i i i 个数的构造,其中 B i = j B_i=j Bi=j 时, S S S 的最小值。
状态转移方程: F [ i , j ] = min ⁡ 0 ≤ k ≤ j { F [ i − 1 , k ] + ∣ A i − j ∣ } F[i,j]=\min\limits_{0\leq k\leq j}\{F[i-1,k]+|A_i-j|\} F[i,j]=0kjmin{F[i1,k]+Aij}
我们可以将 A A A 离散化,并且记录决策集合中的最小值,时间复杂度 O ( N 2 ) O(N^2) O(N2)

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=2e3+10;
const int INF=0x3f3f3f3f;
int a[N],b[N],n,f[N][N],ans=INF;
int main()
{
	//freopen("in.txt","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),b[i]=a[i];
	sort(b+1,b+n+1);
	for(int i=1;i<=n;i++)
	{
		int tmp=INF;
		for(int j=1;j<=n;j++)
		{
			tmp=min(tmp,f[i-1][j]);
			f[i][j]=tmp+abs(a[i]-b[j]);
		}
	}
	for(int i=1;i<=n;i++)
	    ans=min(ans,f[n][i]);
	printf("%d\n",ans);
	return 0;
}

总结

这道DP题中,最关键的就是那个引理,然后就是状态的定义和转移时根据状态集合只增不减的性质减少遍历。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值