POJ 3666 Making the Grade
发布日期:2022-02-05 18:27:27 浏览次数:16 分类:技术文章

本文共 4023 字,大约阅读时间需要 13 分钟。

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

|
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

71324539

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
#include
#include
#include
#include
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<
<<'\n'; return 0;}
//N^2#include
#include
#include
#include
#include
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<
<<'\n'; return 0;}

转载地址:https://blog.csdn.net/li412302070/article/details/12906987 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:POJ 3623 Best Cow Line
下一篇:VIJOS 1008 篝火晚会

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月17日 06时33分56秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章