Poj Making the Grade【dp】

本文探讨了如何通过最小化成本调整道路坡度,使之成为连续的单调斜坡,涉及了动态规划算法的应用。

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

Making the Grade
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5656 Accepted: 2642

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

Source


题意:

给出一个数字串,让你修改某些位置的值,使其变成单调函数(不要求严格单调),且规定修改的消耗是 修改前的值减去修改后的值的绝对值,问最小最小消耗是多少。


题解:

dp 问题确实不会做,参考的大神的代码

膜拜大神


动态规划,最重要的是找到动态转移方程,也就是如何决策才能一步步的实现全局最优解

感觉自己理解的还太少,智商被碾压了....

本题的做法大概是


时间和空间都是 n*n 复杂度的算法:

dp[i][j] 表示 把第 i 个元素修改成第 j 小的元素的最小消耗

需要一个数组进行离散化,实现元素和数值的对应,排序之后,第y[j] 就是第 j 小的数


/*
	http://blog.youkuaiyun.com/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll ABS(ll x)
{
    return x>0?x:-x;
}
ll x[2005],y[2005],dp[2005][2005];
ll slove(ll n)
{
	memset(dp,0,sizeof(dp));
	sort(y+1,y+n+1);
	for(ll i=1;i<=n;++i)
	{
		ll tp=dp[i-1][1];
		for(ll j=1;j<=n;++j)
		{
			tp=min(tp,dp[i-1][j]);
			//ll m=abs((ll)x[i]-y[j]);
			ll m=ABS(x[i]-y[j]);
			dp[i][j]=tp+m;
		}
	}
	ll ans=dp[n][1];
	for(ll i=1;i<=n;++i)
	{
		ans=min(ans,dp[n][i]);
	}
	return ans;
}
int main()
{
	ll n;
	//freopen("shuju.txt","r",stdin);
	while(~scanf("%I64d",&n))
	{
		for(ll i=1;i<=n;++i)
		{
			scanf("%I64d",&x[i]);
			y[i]=x[i];
		}
		printf("%I64d\n",slove(n));
	}
	return 0;
}


空间优化到 n 的算法:

dp[i] 表示把整个序列修改成有序的且最后一个值是x[i] 需要的最少花费(也许理解有误,求指导)


/*
	http://blog.youkuaiyun.com/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
int x[2005],y[2005],dp[2005];
int slove(int n)
{
	sort(y,y+n);
	memset(dp,0,sizeof(dp));
	for(int i=0;i<n;++i)
	{
		int tp=dp[0];
		for(int j=0;j<n;++j)
		{
			tp=min(tp,dp[j]);
			dp[j]=tp+abs(x[i]-y[j]);
		}
	}
	int ans=dp[1];
	for(int i=2;i<n;++i)
	{
		ans=min(ans,dp[i]);
	}
	return ans;
}
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		for(int i=0;i<n;++i)
		{
			scanf("%d",&x[i]);
			y[i]=x[i];
		}
		printf("%d\n",slove(n));
	}
	return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值