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

A B 1| + | A 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

9

Sample Output

题目链接:http://poj.org/problem?id=3666

推荐题解链接:http://www.hankcs.com/program/cpp/poj-3666-making-the-grade.html

**********************************************

题意:给定一个正整数序列a[1...n],要求你改变每一个数变成b[1...n],使得改变后的序列非严格单调,改变的代价为abs(a[1]-b[1])+...+abs(a[n]-b[n]),求代价最小值。

分析:dp+离散化。

显然b[i]必定为a[1...n]中的某个值,且由于a过大,所以离散化。我们将每个数离散化 然后排序。设dp[i][j]为第i个数改变为b[j]时代价最小值,

则dp[i][j]=min(dp[i-1][k])+abs(a[i]-b[j]),然后 b数组表示的就是 离散过的 那些数据。

AC代码:

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
using namespace std;
#define N 12000
#define INF 0x3f3f3f3f int b[N],a[N],dp[N]; int main()
{
int n,i,j; while(scanf("%d", &n) != EOF)
{
memset(dp,,sizeof(dp));
memset(a,,sizeof(a));
memset(b,,sizeof(b)); for(i=;i<n;i++)
{
scanf("%d", &a[i]);
b[i]=a[i];
} sort(b,b+n); for(i=;i<n;i++)
dp[i]=abs(a[]-b[i]); for(i=;i<n;i++)
{
int mi=INF;
for(j=;j<n;j++)
{
mi=min(mi,dp[j]);
dp[j]=mi+abs(a[i]-b[j]);
}
} int minn=INF;
for(i=;i<n;i++)
minn=min(dp[i],minn); printf("%d\n",minn);
}
return ;
}

POJ - 3666 Making the Grade(dp+离散化)的更多相关文章

  1. [poj 3666] Making the Grade (离散化 线性dp)

    今天的第一题(/ω\)! Description A straight dirt road connects two fields on FJ's farm, but it changes eleva ...

  2. poj 3666 Making the Grade(dp)

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

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

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

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

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

  5. poj 3666 Making the Grade(离散化+dp)

    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离散化)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7068   Accepted: 3265 ...

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

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

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

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

  9. POJ 3666 Making the Grade (DP)

    题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...

随机推荐

  1. FireFox站点标识按钮

    Firefox 的站点标识按钮可以为您提供有关您访问的网站的详细信息.通过站点标识按钮,您可以了解到站点的加密信息.验证信息.网站所有者和网站验证者.这有助于避免恶意网站获得您的重要信息. 站点标识按 ...

  2. Python使用shape计算矩阵的行和列

    shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度. 1 >>> a=mat([[1,2,3],[5,6,9]]); 2 >> ...

  3. Confluence搭建

    参见 https://confluence.atlassian.com/display/CONF54/Installing+the+Confluence+EAR-WAR+Edition 下载5.4.4 ...

  4. SOUI入门

    环境:win7_64旗舰版,VS2013 发现了一个比较好用的DirectUI库——SOUI,但是写一个窗口程序不是很方便,程序初始化的地方稍显繁琐,这里稍微封装一下. 大概包含6个文件: SouiC ...

  5. HDU 5806 NanoApe Loves Sequence Ⅱ

    将大于等于m的数改为1,其余的改为0.问题转变成了有多少个区间的区间和>=k.可以枚举起点,二分第一个终点 或者尺取法. #pragma comment(linker, "/STACK ...

  6. 忘记oracle用户名密码怎么办?

    忘记oracle用户名密码怎么办? 忘记了安装时设置的用户名和密码怎么办?查了下网上的资料,终于解决了! 方法一: 首先进入sqlplus:进入的方式有两种,一种是通过cmd命令台输入sqlplus, ...

  7. linux手工笔记

    一.vi编辑器常用命令: -------------------------------- •插入文字:I键A键 •退回命令模式:ESC •存盘:w •不存盘退出:q •存盘退出:wq •强制退出:q ...

  8. @Value 注解获取properties值

    转自:使用Spring 3的@value简化配置文件的读取 Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码. 1.在application ...

  9. git基本命令--tag, alias,

    git tag: 列出标签 在 Git 中列出已有的标签是非常简单直观的. 只需要输入 git tag: $ git tag v0. v1. 这个命令以字母顺序列出标签:但是它们出现的顺序并不重要. ...

  10. mybatis 总结(1)

    注意事项 1.在使用type 和JavaType 以及reusltType ,ofType的时候一定要设置"别名"在mybatis.cfg.xml中设置 <typeAlias ...