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. epoll详解

    转自:http://blog.chinaunix.net/uid-24517549-id-4051156.html 什么是epoll epoll是什么?按照man手册的说法:是为处理大批量句柄而作了改 ...

  2. 1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积。int getCircumference():获得图形的周长 (2)编写一个圆类Circle,要求:圆类Circle实现接口ShapePara。 该类包含有成员变量: radius:public 修饰的double类型radius,表示圆的半径。 x:private修饰的double型变量

    //接口 ShapePara package d922B; public interface ShapePara { int getArea(); int getCircumference(); } ...

  3. android 编译的原理介绍

    http://blog.csdn.net/mr_raptor/article/details/7540066

  4. MS13-069(CVE-2013-3205) CCaret use-after-free Vulnerability Analysis (2014.9)

    MS13-069(CVE-2013-3205) CCaret use-after-free Vulnerability Analysis 1. Introduction In IE's standar ...

  5. C++侵入式链表

    C++标准模板库中的list是非侵入式的链表,当我们通过对象来删除容器中的对象时,需要从头到尾查找一次得到iterator,最后通过iterator来删除对象.这样删除容器中的对象时比较缓慢,所以就实 ...

  6. db2安装要设置tcp、ip

    1.注册表变量DB2COMM是否已经设置了值,是什么级别的?db2set -all | grep -i "DB2COMM" (in unix like os)db2set -all ...

  7. Chapter 1 First Sight——34

    "Was that the boy I sat next to in Biology?" I asked artlessly. 你是生物课坐在我旁边的男生吗?我天真烂漫的问道. & ...

  8. SELECT TOP 1 * FROM是什么意思

    SELECT TOP 1 * FROM的含义: 1.select为命令动词,含义为执行数据查询操作: 2.top 1子句含义为查询结果只显示首条记录: 3.*子句表示查询结果包括数据源中的所有字段: ...

  9. 第十三节,基本数据类型,数字int字符串str

    基本数据类型 数字 int 字符串 str 布尔值 bool 列表 list 元组 tuple 字典 dict 数据类型关系图 查看一个对象的类 如:如查看对象变量a是什么类          用到函 ...

  10. java 线程池 并行 执行

    https://github.com/donaldlee2008/JerryMultiThread/blob/master/src/com/jerry/threadpool/ThreadPoolTes ...