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

Making the Grade
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9118   Accepted: 4261

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

|A1 - B1| + |A2 - B2| + ... + |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题,做了好久,感觉自己dp做的太少了。。。言归正传,首先这道题可以想到的是每一次变成的新数一定在原序列中(可以用贪心的思想来想)。用dp[i][j],i表示当前处理到第i个数,j表示在b序列(见代码)中排第j的数。因为每一个高度可能达到10亿,所以需要离散化。将它们存入b中,并排序。当然,二维dp可以用滚动数组优化成一维。

在这里附上两种方法的效率比较。

(二维数组)

(滚动数组)

确实省了很多。。。

代码

//二维数组
#include<cstdio>
#include<algorithm>
#include<cstdlib>
typedef long long ll;
using namespace std;
ll a[2010];
ll b[2010];
ll n;
ll dp[2010][2010];
bool cmp(ll a,ll b)
{return a>b;
}
ll solveup()
{sort(b,b+n);for(int k=0;k<n;k++)dp[0][k]=abs(a[0]-b[k]);ll v=0;for(int i=1;i<n;i++){v=dp[i-1][0];for(int j=0;j<n;j++){v=min(v,dp[i-1][j]);dp[i][j]=v+abs(a[i]-b[j]);} }v=dp[n-1][0];for(int i=0;i<n;i++)v=min(v,dp[n-1][i]);return v;
}
ll solvedown()
{sort(b,b+n,cmp);for(int k=0;k<n;k++)dp[0][k]=abs(a[0]-b[k]);ll v=0;for(int i=1;i<n;i++){v=dp[i-1][0];for(int j=0;j<n;j++){v=min(v,dp[i-1][j]);dp[i][j]=v+abs(a[i]-b[j]);} }v=dp[n-1][0];for(int i=0;i<n;i++)v=min(v,dp[n-1][i]);return v;
}
int main()
{scanf("%lld\n",&n);for(int i=0;i<n;i++){scanf("%lld",&a[i]);b[i]=a[i];}printf("%lld\n",min(solveup(),solvedown()));return 0;
} 
//滚动数组
#include<cstdio>
#include<algorithm>
#include<cstdlib>
typedef long long ll;
using namespace std;
ll a[2010];
ll b[2010];
ll n;
ll dp[2010];
bool cmp(ll a,ll b)
{return a>b;
}
ll solveup()
{sort(b,b+n);for(int k=0;k<n;k++)dp[k]=abs(a[0]-b[k]);ll v=0;for(int i=1;i<n;i++){v=dp[0];for(int j=0;j<n;j++){v=min(v,dp[j]);dp[j]=v+abs(a[i]-b[j]);} }v=dp[0];for(int i=0;i<n;i++)v=min(v,dp[i]);return v;
}
ll solvedown()
{sort(b,b+n,cmp);for(int k=0;k<n;k++)dp[k]=abs(a[0]-b[k]);ll v=0;for(int i=1;i<n;i++){v=dp[0];for(int j=0;j<n;j++){v=min(v,dp[j]);dp[j]=v+abs(a[i]-b[j]);} }v=dp[0];for(int i=0;i<n;i++)v=min(v,dp[i]);return v;
}
int main()
{scanf("%lld\n",&n);for(int i=0;i<n;i++){scanf("%lld",&a[i]);b[i]=a[i];}printf("%lld\n",min(solveup(),solvedown()));return 0;
} 

poj3666(基础dp+离散化)相关推荐

  1. poj3666(DP+离散化)

    题目链接:http://poj.org/problem?id=3666 思路: 看了讨论区说本题的数据比较弱,只需要考虑不减序列即可,比较懒,所以我也只写了这一部分的代码,思路都一样,能AC就行了. ...

  2. kuangbin专题十二 基础DP

    kuangbin专题十二 基础DP A - HDU1024 Max Sum Plus Plus B - HDU1029 Ignatius and the Princess IV C - HDU1069 ...

  3. POJ3666 线性dp+维度优化

    POJ3666 线性dp+维度优化 题面 POJ3666 题面 思路 首先是重要的归纳法寻求思路的思想,在满足S最小化的前提下,假设存在一种构造前k位序列B的方案,即已经构造B1B2⋯BkB_1B_2 ...

  4. 【算法模板】动态规划(基础DP篇)

    [算法模板]动态规划(基础DP篇)

  5. *POJ3666.Making the Grade(DP+离散化)

    题目链接:http://poj.org/problem?id=3666 题意:求将一个序列调整成纯单调(可以相等)的序列所需要的最小代价 解题思路: 分两种情况讨论: ①不减序列:dp[i][j]表示 ...

  6. 【Kuangbin 带你飞系列】 基础dp

    dp好难啊啊啊啊啊啊啊啊啊啊 HDU1024 Max Sum Plus Plus 题目大意就是给你一个序列从里面截出连续m段使每一段区间不相交并且和最大 思路就是: 集合表示 :我们先确定状态dp[i ...

  7. 团队暑期集训 - 基础DP练习

    目录 A - Max Sum Plus Plus D - 做作业(状态压缩) E - Super Jumping! Jumping! Jumping! I - 最少拦截系统(最长上升子序列模板) 团队 ...

  8. 过河(dp+离散化)

    过河 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点 ...

  9. HDU 6447 YJJ's Salesman(树状数组优化DP + 离散化)

    HDU 6447 YJJ's Salesman 题目 给一个二维数组,从(0,0)走到(1e9, 1e9).每次只能走右,下,右下,三个方向.其中只有通过右下走到特定给出的点(村庄)时才会获得分值.问 ...

最新文章

  1. MLIR算子量化Quantization
  2. 6.字符串解析(LeetCode第394题)
  3. python微信自动机器人
  4. 基于Blink构建亲听项目以及全链路debug项目实时响应能力
  5. 串灯控制盒去掉怎么接_单双向可控硅好坏怎么判断
  6. 【2017年第1期】基于外卖物流配送大数据的调度系统
  7. BZOJ4012[HNOI2015]开店——树链剖分+可持久化线段树/动态点分治+vector
  8. [转载]C# Socket编程 同步以及异步通信
  9. centos 7安装搭建confluence-wiki
  10. 人口各省预测模型matlab_利用matlab编程求解人口预测模型.doc
  11. 工业机器人图册 索罗门采夫_机械手控制电路 机电一体化毕业设计论文.doc
  12. 程序员,不甘平凡又害怕努力…
  13. 重读Ardupilot中stabilize model+MAVLINK解包过程
  14. 算法快学笔记(九):红黑二叉树
  15. 2020最新版影评小程序搭建教程(附源码获取渠道)
  16. uni-app做app自定义弹窗实现
  17. 导入数据库显示服务器发生意外,mysql 数据库无法启动(Ignoring the redo log due to missing M...
  18. 强化学习(1)-介绍
  19. OPENWRT系统学习系列之一(系统源码到编译固件和烧录固件)
  20. Python爬取wallhaven壁纸 2023.1.31

热门文章

  1. Java 中的屏幕共享
  2. Python之OpenGL笔记(38):三种光照通道的合成
  3. JSP中文乱码问题终极解决方案(上)
  4. 【ML30】Basic K-means clustering algorithm
  5. 年终重磅盘点:2022计算机科学6大突破!破解量子加密、最快矩阵乘法等榜上有名...
  6. TX2上的Raspberry Pi相机
  7. HTTP协议的请求格式解析
  8. Raft源码分析(二) - Role转换
  9. Unity学习——音效系统+音频过滤器+音频混响区+音频管理器
  10. linux scp 详解