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 1 - B 1| + | A 2 - 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

题解:
  这个题目我们先考虑暴力,dp[i][j]表示dp到i这一位,最后一个数是j的最小花费,那么状态数是n*(l~r),显然是不行的。
  考虑一个小优化,因为只有数j在序列中出现过才会有用,所以第二维可以改为第j大的数,这样子状态数就是n^2级别的了,转移是(上升)dp[i][j]=min(dp[i-1][1~j])+abs(hi[i]-hi[j)。
  把这个式子列出来就知道怎么优化转移了,计D[i][j]=min(dp[i][1~j]),那么转移就是D[i][j]=min*+(D[i][j-1],dp[i][j]),转移就变成O(n)的了。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 2020
#define inf 0x3f3f3f3f3f
#define ll long long
using namespace std;
ll dp[MAXN][MAXN],last[MAXN],hi[MAXN],rak[MAXN],D[MAXN][MAXN],ans=inf;
int n; ll abss(ll x){
if(x<) return -x;
return x;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%lld",&hi[i]),rak[i]=hi[i];
sort(rak+,rak+n+);
memset(D,inf,sizeof(D));
memset(dp,inf,sizeof(dp));
for(int i=;i<=n;i++) dp[][i]=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(i==) dp[i][j]=abss(hi[i]-rak[j]);
else dp[i][j]=D[i-][j]+abss(hi[i]-rak[j]);
D[i][j]=min(D[i][j-],dp[i][j]);
}
for(int i=;i<=n;i++) ans=min(ans,dp[n][i]);
memset(dp,inf,sizeof(dp));
memset(D,inf,sizeof(D));
for(int i=;i<=n;i++) dp[][i]=;
for(int i=;i<=n;i++)
for(int j=n;j>=;j--){
if(i==) dp[i][j]=abss(hi[i]-rak[j]);
else dp[i][j]=D[i-][j]+abss(hi[i]-rak[j]);
D[i][j]=min(D[i][j+],dp[i][j]);
}
for(int i=;i<=n;i++) ans=min(ans,dp[n][i]);
printf("%lld\n",ans);
return ;
}

Making the Grade POJ - 3666的更多相关文章

  1. S - Making the Grade POJ - 3666 结论 将严格递减转化成非严格的

    S - Making the Grade POJ - 3666 这个题目要求把一个给定的序列变成递增或者递减序列的最小代价. 这个是一个dp,对于这个dp的定义我觉得不是很好想,如果第一次碰到的话. ...

  2. A-Making the Grade(POJ 3666)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4656   Accepted: 2206 ...

  3. DP:Making the Grade(POJ 3666)

     聪明的修路方案 题目大意:就是农夫要修一条路,现在要求这条路要么就是上升的,要么就是下降的,总代价为∑|a[i]-b[i]|,求代价最低的修路方案, (0 ≤ β≤ 1,000,000,000) , ...

  4. Poj 3666 Making the Grade (排序+dp)

    题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...

  5. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

  6. 把一个序列转换成非严格递增序列的最小花费 POJ 3666

    //把一个序列转换成非严格递增序列的最小花费 POJ 3666 //dp[i][j]:把第i个数转成第j小的数,最小花费 #include <iostream> #include < ...

  7. POJ - 3666 Making the Grade(dp+离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  8. POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)

    传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total ...

  9. POJ 3666 Making the Grade(二维DP)

    题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...

随机推荐

  1. 每天学会一点点(spring-mvc.xml与web.xml配置文件)

    1.spring-mvc.xml中拦截器的使用 首先在springMVC.xml配置如下代码: <!-- 拦截器 --> <mvc:interceptors> <bean ...

  2. 我的MarkDown入门

    目录 0.前言 1.软件准备 2.基本语法 2.1斜体&加粗 2.2分级标题 2.3分割线 2.4超链接 2.5列表 2.6引用 2.7插入代码 2.8插入图像 2.9插入表格 2.10目录 ...

  3. Java web 修改默认web部署路径

    转载自:https://blog.csdn.net/lcczpp/article/details/79968070 在eclipse上面部署web项目后,它没有将你的项目文件放到tomcat 的目录下 ...

  4. Redis在新项目中的使用场景

    Redis在新项目中的使用场景 数据类型 使用场景 string 比如说,我想知道什么时候封锁一个Ip地址,Incrby命令(使用这个命令记录被访问的次数) Hash 存储用户的信息[id,name, ...

  5. Winform中怎样根据Name获取同窗体的控件

    场景 在同一个Winform窗体中,点击一个Button按钮时, 获取同窗体的其他控件的属性. 首先需要对要获取的控件赋予Name属性,然后就可以通过Name进行获取. 实现 在Button的点击事件 ...

  6. Java如何创建不存在的指定路径的文件?

    实际应用中,要在指定位置创建一个文件,但文件及文件之前的目录都不存在,此时可用以下方法进行创建. 以下是主要代码: File f = new File("I:" + File.se ...

  7. 利用github搭建私人maven仓库

    一.背景 最近在做HBase的项目,不免会引用到一些工具类,如StringUtils,NumberUtils,DateUtils这些,公司底层有封装好可以直接使用. 但是项目完成,用maven打包部署 ...

  8. 深入浅出Mysql索引的那些事儿

    文章来源:公众号:猿人谷 一.索引的作用 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,所以查询语句的优 ...

  9. nginx主配置参数详解

    ########Nginx的main(全局配置)文件 #指定nginx运行的用户及用户组,默认为nobody #user nobody; #开启的线程数,一般跟逻辑CPU核数一致 worker_pro ...

  10. 删除linux自带jdk

    提示:error: can't create transaction lock on /var/lib/rpm/.rpm.lock (Permission denied):代表权限不够 执行:su r ...