POJ 3666 Making the Grade

本文介绍了一种解决道路整平问题的算法优化方案,通过动态规划的方法实现了对道路高低差的有效调整,使得整条道路形成单一斜率,既节省成本又符合使用需求。
Making the Grade
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3425 Accepted: 1601

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

|AB1| + |AB2| + ... + |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



一开始想到的是贪心
从头开始不下降
从头开始不上升
从尾开始不下降
从尾开始不上升
这样每一次不是全部砍掉就是全部向上建造
最后得分40分


讲解完后了解到这是个dp题
先算不下降的
f[i][j]表示第i个高度j时的最小花费
丧心病狂的数据 0 ≤ j ≤ 1,000,000,000
但是发现最多只有2,000个建筑
即最多只有2000个不同的高度
可以枚举出现的这些高度 因为调整后的高度一定是出现过的高度 这样才是最优的

可以先读入每一个高度h[i] 排序后存入t中
f[i][j]=min(f[i-1][k])+abs(h[i]-t[j])
k<=j
这样是n^3的dp 需要循环i j k 可以得到50分

可以用g[i][j]维护min(f[i-1][k])
这样可以降低到n^2 可以过了
g[i][1]=f[i][1]
g[i][k]=min(g[i][k-1],f[i][k]),k>1


然后可以倒着做不下降 也可以正着做不上升 求最小值即为答案


//N^3
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

int h[2022],t[2022];
int g[2022][2022],f[2022][2022];

int m,n,a,b,c,z;

int uplis(int i,int tall)
{
    if(i==0||tall<0)return 0;
    if(f[i][tall]!=-1)return f[i][tall];
    f[i][tall]=uplis(i-1,1)+abs(t[tall]-h[i]);
    for(int a=2;a<=tall;a++)
    f[i][tall]=min(f[i][tall],uplis(i-1,a)+abs(t[tall]-h[i]));
    return f[i][tall];
}

int downlis(int i,int tall)
{
    if(i==0||tall<0)return 0;
    if(f[i][tall]!=-1)return f[i][tall];
    f[i][tall]=downlis(i-1,m)+abs(t[tall]-h[i]);
    for(int a=m-1;a>=tall;a--)
    f[i][tall]=min(f[i][tall],downlis(i-1,a)+abs(t[tall]-h[i]));
    return f[i][tall];
}

int main()
{
    scanf("%d",&m);
    for(a=1;a<=m;a++){scanf("%d",&h[a]);t[a]=h[a];}
    sort(t+1,t+m+1);
    
    memset(f,-1,sizeof(f));
    memset(g,-1,sizeof(f));
    z=uplis(m,1);
    for(a=2;a<=m;a++)z=min(z,uplis(m,a));
    
    memset(f,-1,sizeof(f));
    memset(g,-1,sizeof(f));
    for(a=1;a<=m;a++)z=min(z,downlis(m,a));
    
    cout<<z<<'\n';
    return 0;
}


//N^2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

int h[2022],t[2022];
int g[2022][2022],f[2022][2022];

int m,n,a,b,c,z=999999999;

void uplis()
{
    memset(f,0,sizeof(f));
    memset(g,0,sizeof(g));
    int i,j;
    for(i=1;i<=m;++i)
        for(j=1;j<=m;++j)
        {
            f[i][j]=g[i-1][j]+abs(h[i]-t[j]);
            if(j==1)g[i][j]=f[i][j];
            else g[i][j]=min(f[i][j],g[i][j-1]);
        }
    for(int a=1;a<=m;a++)z=min(z,f[m][a]);
}

void downlis()
{
    memset(f,0,sizeof(f));
    memset(g,0,sizeof(g));
    int i,j;
    for(i=1;i<=m;++i)
        for(j=m;j>=1;--j)
        {
            f[i][j]=g[i-1][j]+abs(h[i]-t[j]);
            if(j==m)g[i][j]=f[i][j];
            else g[i][j]=min(f[i][j],g[i][j+1]);
        }
    for(int a=1;a<=m;a++)z=min(z,f[m][a]);
}

int main()
{
    scanf("%d",&m);
    for(a=1;a<=m;a++){scanf("%d",&h[a]);t[a]=h[a];}
    sort(t+1,t+m+1);
    uplis();
    downlis();
    cout<<z<<'\n';
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值