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

显然这题的难点在于抉择第i点到底提升自己还是降低之前的
那么干脆就把所有可能考虑到 用dp[i][j]表示 第i点以j结尾的最小cost
但是题中给的数据量来看 这个数组实在太大 所以再加上离散化 那么就是O(n^2)的方法了 这题数据很水 只要非降序就能过
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; int n, arry[], cast[];
int dp[][]; int main()
{
ios::sync_with_stdio(false);
while(cin >> n){
for(int i = ; i < n; ++i){
cin >> arry[i];
}
memcpy(cast, arry, sizeof arry);
sort(cast, cast + n); for(int i = ; i < n; i++){
dp[][i] = abs(arry[] - cast[i]);
} for(int i = ; i < n; i++){
int mini = dp[i-][];
for(int j = ; j < n; j++){
mini = min(dp[i-][j], mini);
dp[i][j] = abs(arry[i] - cast[j]) + mini;
}
} cout << *min_element(dp[n-], dp[n-] + n) << endl;
}
return ;
}

kaungbin_DP S (POJ 3666) Making the Grade的更多相关文章

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

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

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

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

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

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

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

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

  5. POJ 3666 Making the Grade

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

  6. poj 3666 Making the Grade(dp)

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

  7. POJ 3666 Making the Grade (动态规划)

    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)

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

  9. POJ 3666 Making the Grade (线性dp,离散化)

    Making the Grade Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) T ...

随机推荐

  1. 解决Inno Setup制作中文安装包在非中文系统上显示乱码的问题

    尼玛,好几个月没更新了.囧... 目前我司新的客户端开发已经接近尾声,该改的bug已经改完,该重构的地方也都差不多了.视觉效果也已经根据美工的样式改完了.所以,就差制作安装包了.正所谓万事俱备,只欠东 ...

  2. java数据结构_附12_图、顶点和边的定义(双链存储)

    图--双链式存储结构 顶点 和 边 的定义 1.Vertex.java 2.Edge.java 3.AbstractGraph.java 1. public class Vertex {private ...

  3. 【python】操作excel——xlrd xlwt xlutils

    from xlutils.copy import copy import xlrd # import xlutils #打开已存在的excel rb=xlrd.open_workbook('D:\\1 ...

  4. EasyUI使用JSON保存数据

    目前来说,使用JSON保存数据比较方便,前台可以不用Test.aspx 页面,可以直接用Html页面,使用.aspx页面的弊端就不在这里熬述. 具体步骤如下: 1.新建一个Html页面,命名为Test ...

  5. DOM扩展之 专有扩展

    11.4.3 contains() 方法 用来确定某个节点是不是另一个节点的后代. 注:a.contains(a) 也是返回true.说明contains方法搜索是从自身开始的. DOM Level ...

  6. MFC之TreeCtrl控件使用经验总结

    树形控件可以用于树形的结构,其中有一个根接点(Root)然后下面有许多子结点,而每个子结点上有允许有一个或多个或没有子结点.MFC中使用CTreeCtrl类来封装树形控件的各种操作.通过调用BOOL ...

  7. jQuery从无知到无所不知

    说明: 本文以读书笔记的方式整理jQuery的重要知识点,言简意赅,一语中的,删繁就简,引人入胜. 本文先对jQuery强大的功能作一系统的介绍,再详细整理jQuery的选择器.过滤器.DOM操作.事 ...

  8. [GodLove]Wine93 Tarining Round #7

    比赛链接: http://vjudge.net/contest/view.action?cid=47643#overview 比赛来源: 2012 ACM/ICPC Asia Regional Han ...

  9. linux驱动之LCD

    LCD程序步骤:1. 分配一个fb_info 2. 设置 3. 硬件相关的操作4. 注册 register_framebuffer 5.入口函数 6.出口函数 #include <linux/s ...

  10. The Swift Programming Language 中文翻译版(个人翻新随时跟新)

    The Swift Programming Language --lkvt 本人在2014年6月3日(北京时间)凌晨起来通过网络观看2014年WWDC 苹果公司的发布会有iOS8以及OS X 10.1 ...