poj3666(基础dp+离散化)

版权声明:本文为博主原创文章,转载请注明出处,谢谢! https://blog.csdn.net/star_moon0309/article/details/80456155

题目链接:http://poj.org/problem?id=3666

Making the Grade
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9118 Accepted: 4261

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

思路:一道基础dp题,做了好久,感觉自己dp做的太少了。。。言归正传,首先这道题可以想到的是每一次变成的新数一定在原序列中(可以用贪心的思想来想)。用dp[i][j],i表示当前处理到第i个数,j表示在b序列(见代码)中排第j的数。因为每一个高度可能达到10亿,所以需要离散化。将它们存入b中,并排序。当然,二维dp可以用滚动数组优化成一维。

在这里附上两种方法的效率比较。

                                                               (二维数组)

                                 

                                                               (滚动数组)

                                 

确实省了很多。。。

代码

//二维数组 
#include<cstdio>
#include<algorithm>
#include<cstdlib>
typedef long long ll;
using namespace std;
ll a[2010];
ll b[2010];
ll n;
ll dp[2010][2010];
bool cmp(ll a,ll b)
{
  return a>b;
}
ll solveup()
{
	sort(b,b+n);
	for(int k=0;k<n;k++)
	dp[0][k]=abs(a[0]-b[k]);
	ll v=0;
	for(int i=1;i<n;i++)
	{
		v=dp[i-1][0];
		for(int j=0;j<n;j++)
		{
			v=min(v,dp[i-1][j]);
			dp[i][j]=v+abs(a[i]-b[j]);
		 } 
	}
     v=dp[n-1][0];
	for(int i=0;i<n;i++)
	v=min(v,dp[n-1][i]);
	return v;
}
ll solvedown()
{
	sort(b,b+n,cmp);
	for(int k=0;k<n;k++)
	dp[0][k]=abs(a[0]-b[k]);
	ll v=0;
	for(int i=1;i<n;i++)
	{
		v=dp[i-1][0];
		for(int j=0;j<n;j++)
		{
			v=min(v,dp[i-1][j]);
			dp[i][j]=v+abs(a[i]-b[j]);
		 } 
	}
	v=dp[n-1][0];
	for(int i=0;i<n;i++)
	v=min(v,dp[n-1][i]);
	return v;
}
int main()
{
	scanf("%lld\n",&n);
	for(int i=0;i<n;i++)
	{
	  scanf("%lld",&a[i]);
	  b[i]=a[i];
    }
	printf("%lld\n",min(solveup(),solvedown()));
	return 0;
} 

//滚动数组 
#include<cstdio>
#include<algorithm>
#include<cstdlib>
typedef long long ll;
using namespace std;
ll a[2010];
ll b[2010];
ll n;
ll dp[2010];
bool cmp(ll a,ll b)
{
  return a>b;
}
ll solveup()
{
	sort(b,b+n);
	for(int k=0;k<n;k++)
	dp[k]=abs(a[0]-b[k]);
	ll v=0;
	for(int i=1;i<n;i++)
	{
		v=dp[0];
		for(int j=0;j<n;j++)
		{
			v=min(v,dp[j]);
			dp[j]=v+abs(a[i]-b[j]);
		 } 
	}
     v=dp[0];
	for(int i=0;i<n;i++)
	v=min(v,dp[i]);
	return v;
}
ll solvedown()
{
	sort(b,b+n,cmp);
	for(int k=0;k<n;k++)
	dp[k]=abs(a[0]-b[k]);
	ll v=0;
	for(int i=1;i<n;i++)
	{
		v=dp[0];
		for(int j=0;j<n;j++)
		{
			v=min(v,dp[j]);
			dp[j]=v+abs(a[i]-b[j]);
		 } 
	}
     v=dp[0];
	for(int i=0;i<n;i++)
	v=min(v,dp[i]);
	return v;
}
int main()
{
	scanf("%lld\n",&n);
	for(int i=0;i<n;i++)
	{
	  scanf("%lld",&a[i]);
	  b[i]=a[i];
    }
	printf("%lld\n",min(solveup(),solvedown()));
	return 0;
} 

展开阅读全文

没有更多推荐了,返回首页