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

【题意】给出一个序列,求以最小代价改成单调不下降序列或单调不上升序列。这里只求单调不减序列。

【思路】dp[i][j]=min(dp[i-1][j]|1<=j<=n)+abs(a[i]-b[j]);

dp[i][j]前i个数,最大为b[j]时的最小代价

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#define inf 0x7fffffff
#define ll long long
#define get_abs(a) ((a)>0?(a):-(a))
using namespace std;
const int N=;
ll a[N],b[N];
long long int dp[N][N]; int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
memset(dp,,sizeof(dp));
ll minx,ans=inf;
for(int i=; i<=n; i++)
{
minx=dp[i-][];
for(int j=; j<=n; j++)
{
minx=min(minx,dp[i-][j]);
dp[i][j]=minx+get_abs(a[i]-b[j]);
}
}
for(int i=; i<=n; i++)
{
ans=min(ans,dp[n][i]);
}
cout<<ans<<endl;
}
return ;
}

Making the Grade_滚动数组&&dp的更多相关文章

  1. Palindrome_滚动数组&&DP

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  2. poj - 1159 - Palindrome(滚动数组dp)

    题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...

  3. HDU 4576 简单概率 + 滚动数组DP(大坑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 坑大发了,居然加 % 也会超时: #include <cstdio> #includ ...

  4. Gym 100507G The Debut Album (滚动数组dp)

    The Debut Album 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/G Description Pop-group & ...

  5. 【滚动数组】 dp poj 1036

    题意:一群匪徒要进入一个酒店.酒店的门有k+1个状态,每个匪徒的参数是:进入时间,符合的状态,携带的钱. 酒店的门刚开始状态0,问最多这个酒店能得到的钱数. 思路: dp数组为DP[T][K]. 转移 ...

  6. BZOJ-1925 地精部落 烧脑DP+滚动数组

    1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...

  7. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  8. [POJ1159]Palindrome(dp,滚动数组)

    题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题 ...

  9. Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)

    题目链接:http://codeforces.com/contest/712/problem/D A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A ...

随机推荐

  1. SAP资产明细报表

    前两年别人写的,无自定义表字段...直接使用: *&---------------------------------------------------------------------* ...

  2. css中的zoom

    CSS中zoom:1的作用兼容IE6.IE7.IE8浏览器,经常会遇到一些问题,可以使用zoom:1来解决,有如下作用:触发IE浏览器的haslayout解决ie下的浮动,margin重叠等一些问题. ...

  3. SQL 创建索引的作用以及如何创建索引

    SQL 创建索引的作用以及如何创建索引 SQL 创建索引的作用 一.使用索引的优点: 1.通过唯一性索引(unique)可确保数据的唯一性 2.加快数据的检索速度 3.加快表之间的连接 4.减少分组和 ...

  4. BZOJ1393 [Ceoi2008]knights

    题意...上ceoi官网看吧... 首先打一下sg函数发现必胜态和必败态的分布位置是有规律的 于是我们只要知道最长步数的必胜态和最长步数的必败态哪个更长就可以了 然后再打一下步数的表...发现必败态的 ...

  5. 【模板下载】innosetup 制作.net安装包的模板

    NetworkComms网络通信框架序言 这个模板是在博客园和CodeProject上的代码修改而成的,感谢原作者 模板是2个 innosetup 制作.net 2.0 安装包的模板 innosetu ...

  6. eclipse隐藏菜单栏实现全部酷黑主题

    将eclipse升级到了最新版的neon,将主题颜色设置为了dark,瞬间高大上了很多,唯独菜单栏还是白色的,很刺眼.况且菜单栏不是很常用,所以我们可以将菜单栏隐藏起来,以达到全部黑色的效果. 步骤: ...

  7. C# 文件与目录的基本操作(System.IO)

    1. 文件操作 /// <summary> /// 文件读写操作 /// 为简化代码供大家学习,暂不考虑捕捉异常 /// </summary> public partial c ...

  8. Windows多线程编程总结

    1 内核对象 1 .1 内核对象的概念 内核对象是内核分配的一个内存块,这种内存块是一个数据结构,表示内核对象的各种特征.并且只能由内核来访问.应用程序若需要访问内核对象,需要通过操作系统提供的函数来 ...

  9. POJ 1062 昂贵的聘礼 最短路 难度:0

    http://poj.org/problem?id=1062 #include <iostream> #include <cstring> #include <queue ...

  10. phpstrom+xdebug+Xdebug helper 调试php

    第一步,php.ini打开xdebug扩展 xdebug.remote_enable=on ; 此地址为IDE所在IP xdebug.remote_host=127.0.0.1 xdebug.remo ...