POJ 3666 Making the Grade (动态规划)

本文介绍了一种算法问题,即如何通过增加或减少数组中的元素值使其变为非递减序列,并计算所需的最小成本。文章提供了两种实现方法,一种为时间复杂度较高的朴素解法,另一种则通过优化达到了更高效的解决方案。

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

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[i][j]表示,将第i个数字,变成第j大的数字所需的最小花费。j实际上就是离散化之后的数组的下标。
dp[i][j]=min(dp[i-1][k]+abs(num[i]-p[k]),dp[i][j]);
其中num是原高度,p是离散化后的数组。k<=j;
但是这样的复杂度是n的三次方,不过还好我们可以用一个数组记录下j之前dp[i-1][k]+abs(num[i]-p[k])的最小值,这样就能优化成n方了。
TLE
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int num[2008],p[2008];
int n;
int dp[2008][2008];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&num[i]);
        p[i]=num[i];
    }
    sort(p+1,p+1+n);
    int m=unique(p+1,p+1+n)-p-1;
    for(int i=1;i<=n;i++){
        int t=lower_bound(p+1,p+1+n,num[i])-p-1;
        for(int j=1;j<=m;j++){
            dp[i][j]=inf;
            for(int k=1;k<=j;k++){
                dp[i][j]=min(dp[i-1][k]+abs(num[i]-p[k]),dp[i][j]);
            }
        }
    }
    printf("%d\n",dp[n][m]);
    return 0;
}
View Code
AC
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int num[2008],p[2008];
int n;
int dp[2008][2008];
int minn[2008];
int main()
{
//    ios::sync_with_stdio(false);
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&num[i]);
        p[i]=num[i];
    }
    sort(p+1,p+1+n);
    int m=unique(p+1,p+1+n)-p-1;
    for(int i=1;i<=n;i++){
        int t=lower_bound(p+1,p+1+n,num[i])-p-1;
        minn[0]=inf;
        for(int j=1;j<=m;j++){
            minn[j]=min(minn[j-1],dp[i-1][j]+abs(num[i]-p[j]));
        }
        for(int j=1;j<=m;j++){
            dp[i][j]=minn[j];
        }
    }
    printf("%d\n",dp[n][m]);
    return 0;
}
View Code

转载于:https://www.cnblogs.com/ZGQblogs/p/10672660.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值