【NOIP模拟题】【动态规划DP】2016.11.11第二题Landscaping题解

2. Landscaping
题目描述
N(1 <= N <= 100)个数排成一行,值分别为A_i,现在希望把每个数对应地改成B_i。(A_i,B_i的值均在0..10之间)。改变的方式有3种:
(1)把A_i增加到B_i,每增加1,花费$X
(2)把A_i减少到B_i,每减少1,花费$Y
(3)把第i个数的值转移到第j个数,每转移1,花费为$Z*|i-j|
问:最小的花费是多少。
输入
第1行:4个整数 N, X, Y, Z (0 <= X, Y, Z <= 1000).
第2..1+N行: 每行2个整数 A_i 和 B_i.
输出
第1行:1个整数,表示最小的花费。
样例输入
4 100 200 1
1 4
2 3
3 2
4 0
样例输出
210
提示
INPUT DETAILS: There are 4 flowerbeds in a row, initially with 1, 2, 3, and 4 units of dirt. Farmer John wishes to transform them so they have 4, 3, 2, and 0 units of dirt, respectively. The costs for adding, removing, and transporting dirt are 100, 200, and 1.
OUTPUT DETAILS: One unit of dirt must be removed (from flowerbed #4), at a cost of 200. The remaining dirt can be moved at a cost of 10 (3 units from flowerbed #4 to flowerbed #1, 1 unit from flowerbed #3 to flowerbed #2).

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

我们·不难看出这是一道DP的题,前两个转移方程也不难想,主要的处理是第三个比较难想。

因为第三种转移花费为Z*|i-j|,所以其值就是下标差的绝对值*z。

但是神奇的std给出了另一种解法,将它转化为编辑距离(Edit Distance)于是得到三个方程:

dp[i][j] = min(dp[i][j], dp[i][j-1] + x);
dp[i][j] = min(dp[i][j], dp[i-1][j] + y);
dp[i][j] = min(dp[i][j], dp[i-1][j-1] + z*abs(A[i]-B[j]));

实在是巧。

那么剩下的就非常好做了。附std代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>

using namespace std; 

const int maxn = 1005;
int n,x,y,z;
int a,b,tot_a,tot_b;
int A[maxn],B[maxn],dp[maxn][maxn];


template <class T> inline void read(T &xx)
{
	xx = 0;
	T flag = 1;
	char ch = (char)getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') flag = -1;
		ch = (char)getchar();
	}
	while(ch>='0' && ch<='9')
	{
		xx = (xx<<1) + (xx<<3) + ch - '0';
		ch = (char)getchar();
	}
	xx *= flag;
}

void inti()
{
	read(n); read(x); read(y); read(z);
	for(int i = 1; i <= n; i++)
	{
		read(a); read(b);
		while(a > 0) 
		{
			A[++tot_a] = i;
			a--;
		}
		while(b > 0)
		{
			B[++tot_b] = i;
			b--;
		}
	}
	for(int i = 0; i <= tot_b; i++)
		dp[0][i] = i * x;
  	for(int i = 0; i <= tot_a; i++)
		dp[i][0] = i * y;
}

void DP()
{
	for(int i = 1; i <= tot_a;  i++)
	    for(int j = 1; j <= tot_b; j++)
		{
			dp[i][j] = 2e9;
			dp[i][j] = min(dp[i][j], dp[i][j-1] + x);
			dp[i][j] = min(dp[i][j], dp[i-1][j] + y);
			dp[i][j] = min(dp[i][j], dp[i-1][j-1] + z * abs(A[i]-B[j]));
	    }
}

int main()
{
	freopen("landscaping.in","r",stdin);
	freopen("landscaping.out","w",stdout);
	inti();
	DP();
	printf("%d\n",dp[tot_a][tot_b]);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值