bzoj 1592 [Usaco2008 Feb]Making the Grade 路面修整
来源:互联网 发布:淘宝店铺设置满包邮 编辑:程序博客网 时间:2024/04/29 18:13
Description
FJ打算好好修一下农场中某条凹凸不平的土路。按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中。 整条路被分成了N段,N个整数
Input
* 第1行: 输入1个整数:N * 第2..N+1行: 第i+1行为1个整数:A_i
Output
- 第1行: 输出1个正整数,表示FJ把路修成高度不上升或高度不下降的最小花费
Sample Input
7
1
3
2
4
5
3
9
Sample Output
3
HINT
FJ将第一个高度为3的路段的高度减少为2,将第二个高度为3的路段的高度增加到5,总花费为|2-3|+|5-3| = 3,并且各路段的高度为一个不下降序列 1,2,2,4,5,5,9。
可以发现一个性质:要改的话肯定改成数组里本来就有的数,那么我们先对原数组进行排序,然后再dp
我们可得朴素dp如下:
{
}
这样复杂度接近
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#define ll long longusing namespace std;ll ans,f[2005][2005];int n;int a[2005],b[2005];bool cmp(int x,int y){ return x>y;}int abs(int x){ if(x<0) return -x;else return x;}int main(){ cin>>n; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); b[i]=a[i]; } sort(b+1,b+n+1); for(int i=1;i<=n;i++) f[i][0]=1e15; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { f[i][j]=1e15; f[i][j]=min(f[i][j],f[i-1][j]+abs(a[i]-b[j])); f[i][j]=min(f[i][j-1],f[i][j]); } ans=f[n][n]; sort(b+1,b+n+1,cmp); for(int i=1;i<=n;i++) f[i][0]=1e15; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { f[i][j]=1e15; f[i][j]=min(f[i][j],f[i-1][j]+abs(a[i]-b[j])); f[i][j]=min(f[i][j-1],f[i][j]); } ans=min(ans,f[n][n]); cout<<ans; return 0;}
- bzoj 1592 [Usaco2008 Feb]Making the Grade 路面修整
- [DP] [Usaco2008 Feb]Making the Grade 路面修整
- 【DP】[Usaco2008 Feb]Making the Grade 路面修整
- BZOJ1592: [Usaco2008 Feb]Making the Grade 路面修整
- bzoj1592 [Usaco2008 Feb]Making the Grade 路面修整 dp
- 【bzoj1592】[Usaco2008 Feb]Making the Grade 路面修整
- BZOJ 1592 Usaco 2008 Feb Making the Grade 路面修整 DP
- 2014.6.14模拟赛【bzoj1592】[Usaco2008 Feb]Making the Grade 路面修整
- [BZOJ 1592] [Usaco2008 Feb] 路面修整
- USACO2008 Feb T4路面修整
- 【bzoj1592】【Usaco2008 Feb】【动态规划】CQYZ_Vijos_P1468 路面修整
- 路面修整
- Making the Grade(poj1527)
- POJ3666---Making the Grade
- Making the Grade
- poj3666 Making the Grade
- poj3666 Making the Grade
- POJ3666-Making the Grade
- Web.py - streaming_large_files
- Search in Rotated Sorted Array
- request对象
- 计算表达式
- 【千里码】Task6-IP
- bzoj 1592 [Usaco2008 Feb]Making the Grade 路面修整
- 浅谈数位DP(以hihocoder #1033 交错和为例)
- 将图像扭正确。利用OpenCV检测图像中的长方形画布或纸张并提取图像内容
- hello world 是我们的第一个脚印!
- LWIP内存池memp.h头文件学习
- Linux mv 重命名文件问题
- 【bzoj1191】【HNOI2006】【超级英雄】【Hero】【二分图匹配】
- [hdu 1711] Number Sequence [kmp]
- GetSystemInfo and VirtualQuery