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. 微服务时代之自定义archetype(模板/骨架/脚手架)

    1. 场景描述 (1)随着微服务越来越常见,一个大的项目会被拆分成多个小的微服务,jar包以及jar之间的版本冲突问题,变得越来越常见,如何保持整体微服务群jar及版本统一,也变成更加重要了,mave ...

  2. FreeSql (二十二)Dto 映射查询

    适合喜欢使用 dto 的朋友,很多时候 entity 与 dto 属性名相同,属性数据又不完全一致. 有的人先查回所有字段数据,再使用 AutoMapper 映射. 我们的功能是先映射,再只查询映射好 ...

  3. 解决chrome浏览器崩溃,再次安装不上问题

    上网重新下载了个安装包,发现安装包都打不来 很绝望,查了很多资料 很多人说要删除注册表的东西 但是打开注册表,发现一堆google的东西,手动删根本不现实 在绝望中看到了解决方案:google Upd ...

  4. Centos7上配置nginx的负载均衡

    前言 在配置nginx负载均衡前.我们需要明白几个名词的概念 注: 如果不小心忘了tomcat和nginx的启动,关闭命令,可参考写在文章最后的命令 一 重要的概念理解 1 什么是nginx呢? Ng ...

  5. Java后端面试经验总结分享(一)

    今天下午两点的时候,我去面了一家招Java开发的公司,本人工作经验2年多一丢丢. 跟大部分公司类似,先做一份笔试题,题目都比较简单,基本都写完了.我把题目以及答案列在下面一下,给自己做一下总结的,也分 ...

  6. ACM讲课之字符串

    本次讲课讲全面介绍字符串以及如何使用字符串解决具体问题. 一.什么是字符串 1.如何存储字符串 平时我们使用的变量有很多,int代表整型变量,double代表浮点型变量,char代表字符型变量,那么对 ...

  7. Scrapy高级用法

    日志相关 一.日志相关变量 LOG_LEVEL = '' LOG_FILE = '文件名.log' 二.日志级别 5 CRITICAL :严重错误 4 ERROR :普通错误 3 WARNING :警 ...

  8. filebeat的@timestamp字段时区问题

    最近使用filebeat进行日志采集,并通过logstash对日志进行格式化处理. filebeat采集数据后,会给日志增加字段@timestamp,@timestamp是UTC时间,查看日志很不方便 ...

  9. 两个变量交换数字 不用第三个变量的情况下 int a = 5,b = 6

    今天可是涨见识额 记录一下 第一种方法: a=a+bb=a-ba=a-b 第二种: b= a+(a=b)*0 一句话搞定

  10. ORM组件LogORM使用指北

    LogORM是一个对数据库进行对象关系映射的ORM组件.当对数据库进行增删改操作时,组件会自动进行日志记录. 该组件支持.Net平台和.NetCore平台,支持SQL Server.Oracle.My ...