您的位置:首页 > 其它

Poj Making the Grade【dp】

2016-04-01 11:53 288 查看
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

|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

Source

USACO 2008 February Gold

题意:

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

题解:

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

膜拜大神

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

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

本题的做法大概是

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

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

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

/* http://blog.csdn.net/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
[1];
for(ll i=1;i<=n;++i)
{
ans=min(ans,dp
[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.csdn.net/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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: