[poj 3666] Making the Grade (离散化 线性dp)
今天的第一题(/ω\)!
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
Source
USACO 2008 February Gold
code:
//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int MAX=2010;
const int INF=0x3f3f3f3f;
int n,a[MAX],num[MAX],f[MAX][MAX];
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
num[i]=a[i];
}
sort(num+1,num+n+1);
int m=unique(num+1,num+n+1)-num-1;
memset(f,0x3f,sizeof f);
f[0][0]=0;
for(int i=1;i<=n;i++) {
int temp=f[i-1][0];
for(int j=1;j<=m;j++) {
temp=min(temp,f[i-1][j]);
f[i][j]=temp+abs(a[i]-num[j]);
}
}
int ans=INF;
for(int i=1;i<=m;i++) ans=min(ans,f[n][i]);
printf("%d",ans);
return 0;
}
[poj 3666] Making the Grade (离散化 线性dp)的更多相关文章
- POJ 3666 Making the Grade (线性dp,离散化)
Making the Grade Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
- poj 3666 Making the Grade(离散化+dp)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- POJ 3666 Making the Grade(二维DP)
题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...
- POJ 3666 Making the Grade(区间dp)
修改序列变成非递减序列,使得目标函数最小.(这题数据有问题,只要求非递减 从左往右考虑,当前a[i]≥前一个数的取值,当固定前一个数的取值的时候我们希望前面操作的花费尽量小. 所以状态可以定义为dp[ ...
- Poj 3666 Making the Grade (排序+dp)
题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...
- POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)
传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 3666 Making the Grade (动态规划)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- POJ - 3666 Making the Grade(dp+离散化)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- poj 3666 Making the Grade(dp离散化)
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7068 Accepted: 3265 ...
随机推荐
- @requestbody @responsebody详解
@requestbody @responsebody详解 会唤起spring mvc的httpmessageconveter转换类进行数据转换 简介: @RequestBody 作用: i) 该注解用 ...
- Project Euler18题 从上往下邻接和
题目:By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ...
- Ruby 写文件
Ruby 写文件 outFile = File.new("fileName.txt","w") outFile.puts "a" outFi ...
- Anaconda安装第三方模块
Anaconda安装第三方模块 普通安装: 进去\Anaconda\Scripts目录,conda install 模块名 源码安装: 进去第三方模块目录,python install setup.p ...
- POJ2230题解
题目来源 id=2230">http://poj.org/problem?id=2230 题目大意 求无向图从起点1開始从不同方向经过全部边的一条路径.输出随意一条. 题解 把无向图的 ...
- vbs脚本
巧用Vbs SendKeys 可以做的事 发布: 2014-04-06 10:00:20 | 作者: | 来源: 按键精灵资源站 巧妙使用VBS中的SendKeys命令(这个命令的作用就是模拟键盘操作 ...
- 一步一步跟我学习lucene(18)---lucene索引时join和查询时join使用演示样例
了解sql的朋友都知道,我们在查询的时候能够採用join查询,即对有一定关联关系的对象进行联合查询来对多维的数据进行整理.这个联合查询的方式挺方便的.跟我们现实生活中的托人找关系类似,我们想要完毕一件 ...
- Hadoop Web项目--Friend Find系统
项目使用软件:Myeclipse10.0,JDK1.7,Hadoop2.6,MySQL5.6.EasyUI1.3.6.jQuery2.0,Spring4.1.3. Hibernate4.3.1,str ...
- js自定义回调函数
JavaScript允许开发人员像传递任何类型的数据一样传递函数,也就是说,函数也是一种数据类型,通过typeof就知道. 例子1: var CallbackFun = function(){ t ...
- vim下很好的右键复制方法
1)先按shift ,然后鼠标选中即可复制:(shift按下时为非vim环境) 2)好方法: "Enable and disable mouse use noremap <f1> ...