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 1 - B 1| + | A 2 - 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
思路:naive的DP解法如下:
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
int i,j,t,n,m=0;
int a[2010],b[2010];
int dp[2010][1000000010];
const int inf = 1<<30;
int main()
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
m=max(m,a[i]);
}
sort(b,b+n);
for(i=0;i<=m;i++)
dp[0][i]=abs(a[i]-i);
// 先写naive的,再优化
for(i=1;i<n;i++)
for(j=0;j<=m;j++)
{
int tmp = inf;
for(k=0;k<j;k++)
tmp=max(tmp,dp[i-1][k]);
dp[i][j]=tmp+abs(a[i]-j)
}
int ret=inf;
for(i=0;i<=m;i++)
ret=min(ret,dp[n-1][i]);
cout<<ret<<endl;
return 0;
}
但是开的空间太大,需要离散化,因为取值就在a数组中的那些,另外开一个数组用index标识就好啦
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
int i,j,k,t,n;
int a[2010],b[2010];
int dp[2010][2010];
const int inf = 1<<30;
int main()
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
}
sort(b,b+n);
for(i=0;i<n;i++)
dp[0][i]=abs(a[0]-b[i]);
// for(i=0;i<n;i++)
// cout<<dp[0][i]<<" ";
// cout<<endl;
// 先写naive的,再优化
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
{
int tmp = inf;
for(k=0;k<=j;k++)
tmp=min(tmp,dp[i-1][k]);
dp[i][j]=tmp+abs(a[i]-b[j]);
}
// for(j=0;j<n;j++)
// cout<<dp[i][j]<<" ";
// cout<<endl;
}
int ret=inf;
for(i=0;i<n;i++)
ret=min(ret,dp[n-1][i]);
cout<<ret<<endl;
return 0;
}
然后DP过程有重复枚举过程,优化下
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
int i,j,t,n,m=0;
int a[2010],b[2010];
int dp[2010][2010];
const int inf = 1<<30;
int main()
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
m=max(m,a[i]);
}
sort(b,b+n);
for(i=0;i<=m;i++)
dp[0][i]=abs(a[i]-i);
// 先写naive的,再优化
for(i=1;i<n;i++)
for(j=0;j<=m;j++)
{
int tmp = inf;
for(k=0;k<=j;k++)
tmp=min(tmp,dp[i-1][k]);
dp[i][j]=tmp+abs(a[i]-j);
}
int ret=inf;
for(i=0;i<=m;i++)
ret=min(ret,dp[n-1][i]);
cout<<ret<<endl;
return 0;
}