[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 ...
随机推荐
- 洛谷 P2412 查单词
P2412 查单词 题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个 ...
- POJ 2486
因为苹果可能在不同的子树中,所以,很容易想到设状态dp_back[i][j]为以i点为树根走j步并回到i点的最大苹果数与dp_to[i][j]不回到i点的两个状态. 于是,转移方程就很明显了.只是注意 ...
- Github Pages 建立过程记录
之前建立过一个測试页面. 如今在折腾CreateJS 试着把离线版的文档传到github pages上面. 第一步:创建Repository 第二步:本地初始化 主要命令:git init 第三步:复 ...
- 基础树形DP小结
HDU 4044 Geodefense http://blog.csdn.net/zmx354/article/details/25109897 树形DP暂且先告一段落了. HDU 3586 Info ...
- CSS艺术之---负margin之美
CSS中负边距(nagative margin)是布局中常常使用的一个技巧.仅仅要运用得当时常会产生奇异的效果.勘称CSS中的奇淫巧计,非常多CSS布局方法都依赖于负边距.掌握它对于前端童鞋来说还是非 ...
- 7种炫酷HTML5 SVG液态水滴融合分解动画特效
这是一组使用HTML5 SVG过滤器制作的炫酷液态水滴融合分解动画特效.这些SVG动画特效使一些HTML元素.如菜单.分页button.APP.选择框等元素的过渡动画像几粒水滴一样融合分解.效果很的酷 ...
- Node.js:模块系统
ylbtech-Node.js:模块系统 1.返回顶部 1. Node.js模块系统 为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统. 模块是Node.js 应用程序的 ...
- [javascript] jQuery系列之目录汇总
最近一个月写了些关于jQuery的文章,谢谢大家的支持.文章仅我个人观点,也许有不对的地方,请指出.这个系列还在更新中 一:jQuery基础系列: jQuery温习篇---强大的JQuery选择器 j ...
- [牛客挑战赛 30D] 小A的昆特牌 解题报告 (组合数学)
interlinkage: https://ac.nowcoder.com/acm/contest/375/D description: solution: 我们枚举步兵的数量$x$,还剩下$S-x$ ...
- centos6.5 + Nat网络模式 +SecureCRT 的相关设置
步骤1:先去查看子网掩码和子网ip 提示:打开后先不要关闭,后边还会使用 步骤2:查看本机名 输入: hostname 步骤3:修改本机名 vi /etc/sysconfig/network 在”Ho ...