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. JS数组方法整理(附使用示例)

    整理目前所用过的数组方法,学习了新增的es6方法. 1.arr.push() 从后面添加元素,返回值为添加完后的数组的长度 let arr = [1,2, is 8.3,4,5] console.lo ...

  2. 基于STM32F429的TFT0.96屏幕驱动

    1.介绍TFT 2.Cube配置  该屏幕是用SPI通信的,但没有MISO引脚,意思是说该屏幕只能接收数据,但无法读取里面的数据,理论上说四线就能启动,但我弄不出,只能用六线. 在Cube上只要开启六 ...

  3. div标签嵌套原则详解(转载)

    这个也许平时人们不注意,但是非常有用,尤其是当你实在找不到原因为什么网页显示错误的时候. XHTML 的标签有许多:div.ul.li.dl.dt.dd.h1~h6.p.a.addressa.span ...

  4. Small Spring系列一:BeanFactory(一)

    人生如逆旅,我亦是行人. 前言 Spring是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用. 准备 bean-v1.xml配置b ...

  5. gh-ost 原理剖析

    gh-ost 原理 一 简介 上一篇文章介绍 gh-ost 参数和具体的使用方法,以及核心特性-可动态调整 暂停,动态修改参数等等.本文分几部分从源码方面解释gh-ost的执行过程,数据迁移,切换细节 ...

  6. java架构之路-(JVM优化与原理)JVM之G1回收器和常见参数配置

    过去的几天里,我把JVM内部的垃圾回收算法和垃圾回收器.还剩下最后一个G1回收器没有说,我们今天数一下G1回收器和常见的参数配置. G1回收器 G1 (Garbage-First)是一款面向服务器的垃 ...

  7. 【Jenkins持续集成(二)】Windows上安装Jenkins教程

    一.前言 Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建.测试和部署软件. Jenkins 支持各种运行方式,可通过系统包.Docker 或者通过一个独立的 Java ...

  8. Nginx 配置Https转发http、 websocket

    系统启动Nginx后,报 [emerg] bind() to 0.0.0.0:XXXX failed (13: Permission denied)错误的处理方式,分为两种: 第一种:端口小于1024 ...

  9. python中pyqt5的进度条--python实战(十)

    python太博大精深了,使用场景非常多.最近笔者一直使用PyQt5编一些小程序,顺便就把一些常用的东西列出来,做个记录和积累吧.进度条是非常常用的东西,今天用的时候,顺便温习了一下,这个东西自己感觉 ...

  10. ELK搭建实时日志分析平台

    ELK搭建实时日志分析平台 导言 ELK由ElasticSearch.Logstash和Kiabana三个开源工具组成,ELK平台可以同时实现日志收集.日志搜索和日志分析的功能.对于生产环境中海量日志 ...