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

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

|AB1| + |AB2| + ... + |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

Source

经典dp

dp[i][j]表示第i个数变成第j小的数的最小消耗。

dp[i][j] = dmin[i - 1][j] + abs(o[i] - d[j]) (ps : dmin[i - 1][j] 表示前i - 1个数并且第i - 1个数小于等于j的最小消耗, abs(o[i] - d[j]) 表示第i个数变成j的消耗);

数据较水,只保证是上升序列即可。

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 2005
ll dp[MAXN][MAXN];
ll dmin[MAXN][MAXN];
ll d[MAXN], o[MAXN];
int main()
{
int n;
scanf("%d", &n);
repu(i, , n + ) { scanf("%I64d", &d[i]); o[i] = d[i]; }
sort(d + , d + n + );
int l = ;
repu(i, , n + ) if(d[i] != d[l]) d[++l] = d[i]; repu(i, , l + ) {
dp[][i] = (ll)abs((int)o[] - (int)d[i]);
if(i == ) dmin[][i] = dp[][i];
else dmin[][i] = min(dp[][i], dmin[][i - ]);
}
ll t = ;
repu(i, , n + ) {
dp[i][] = dp[i - ][] + (ll)abs((int)o[i] - (int)d[]);
dmin[i][] = dp[i][];
repu(j, , l + ) {
dp[i][j] = dmin[i - ][j] + (ll)abs((int)o[i] - (int)d[j]);
dmin[i][j] = min(dp[i][j], dmin[i][j - ]);
}
}
printf("%I64d\n", dmin[n][l]);
return ;
}

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

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

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

  2. Making the Grade POJ - 3666

    A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ would l ...

  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. ServiceStack.OrmLite 学习笔记7-复杂点的使用1

    复杂点的使用1 先看看这2个类 class Customer { public int Id { get; set; } ... } class CustomerAddress { public in ...

  2. 2013/7/16 HNU_训练赛4

    CF328B Sheldon and Ice Pieces 题意:给定一个数字序列,问后面的数字元素能够组成最多的组数. 分析:把2和5,6和9看作是一个元素,然后求出一个最小的组数就可以了. #in ...

  3. 动态CSS--less

    忙了很久终于有时间来写点东西了,不知道大家有没有发现,我们在写CSS的时候总是在重复很多代码,一个相同的属性值往往要重复N次,以前我就经常想有没有什么办法能让我们不用一直重复的font-size啊co ...

  4. [转载] C++ 多线程编程总结

    原文: http://www.cnblogs.com/zhiranok/archive/2012/05/13/cpp_multi_thread.html 在开发C++程序时,一般在吞吐量.并发.实时性 ...

  5. 用CSS样式截取字符串,多的用省略号表示

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  6. (二)shell中case语句、程序传参、while

    2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...

  7. Object Pascal 控制语句

    控制语句 1.常量声明语句常量在声明时就被赋予了一个值,在程序执行过程中是不可改变的. 格式 const 常量名 :数据类型 = 值 下面的例子声明了3 个常量: const Pi = 3.14159 ...

  8. go框架

    beego 的 http server… Author 逆雪寒 2015.12.02 原文地址 https://github.com/nixuehan/beego_you_know/blob/mast ...

  9. Subversion中文手册(svnbook) TortoiseSVN中文帮助手册

    文档地址 http://svndoc.iusesvn.com/

  10. OpenCV图像处理中常用函数汇总(1)

    //俗话说:好记性不如烂笔头 //用到opencv 中的函数时往往会一时记不起这个函数的具体参数怎么设置,故在此将常用函数做一汇总: Mat srcImage = imread("C:/Us ...