A-Making the Grade(POJ 3666)
Making the Grade
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
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 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 Sample Output 3 Source |
经典dp
dp[i][j]表示第i个数变成第j小的数的最小消耗。
dp[i][j] = dmin[i - 1][j] + abs(o[i] - d[j]) (ps : dmin[i - 1][j] 表示前i - 1个数并且第i - 1个数小于等于j的最小消耗, abs(o[i] - d[j]) 表示第i个数变成j的消耗);
数据较水,只保证是上升序列即可。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 2005
ll dp[MAXN][MAXN];
ll dmin[MAXN][MAXN];
ll d[MAXN], o[MAXN];
int main()
{
int n;
scanf("%d", &n);
repu(i, , n + ) { scanf("%I64d", &d[i]); o[i] = d[i]; }
sort(d + , d + n + );
int l = ;
repu(i, , n + ) if(d[i] != d[l]) d[++l] = d[i]; repu(i, , l + ) {
dp[][i] = (ll)abs((int)o[] - (int)d[i]);
if(i == ) dmin[][i] = dp[][i];
else dmin[][i] = min(dp[][i], dmin[][i - ]);
}
ll t = ;
repu(i, , n + ) {
dp[i][] = dp[i - ][] + (ll)abs((int)o[i] - (int)d[]);
dmin[i][] = dp[i][];
repu(j, , l + ) {
dp[i][j] = dmin[i - ][j] + (ll)abs((int)o[i] - (int)d[j]);
dmin[i][j] = min(dp[i][j], dmin[i][j - ]);
}
}
printf("%I64d\n", dmin[n][l]);
return ;
}
A-Making the Grade(POJ 3666)的更多相关文章
- S - Making the Grade POJ - 3666 结论 将严格递减转化成非严格的
S - Making the Grade POJ - 3666 这个题目要求把一个给定的序列变成递增或者递减序列的最小代价. 这个是一个dp,对于这个dp的定义我觉得不是很好想,如果第一次碰到的话. ...
- Making the Grade POJ - 3666
A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ would l ...
- DP:Making the Grade(POJ 3666)
聪明的修路方案 题目大意:就是农夫要修一条路,现在要求这条路要么就是上升的,要么就是下降的,总代价为∑|a[i]-b[i]|,求代价最低的修路方案, (0 ≤ β≤ 1,000,000,000) , ...
- Poj 3666 Making the Grade (排序+dp)
题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...
- 「POJ 3666」Making the Grade 题解(两种做法)
0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...
- 把一个序列转换成非严格递增序列的最小花费 POJ 3666
//把一个序列转换成非严格递增序列的最小花费 POJ 3666 //dp[i][j]:把第i个数转成第j小的数,最小花费 #include <iostream> #include < ...
- 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 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 3666 Making the Grade(二维DP)
题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...
随机推荐
- java运行期类型鉴定
运行期类型识别?RTTI? 假如我们有一个基类的引用,这个引用也可以作为子类的引用嘛,现在我们想知道这个引用的类型到底是啥? 当从子类到基类之后有很多的信息都会丢失掉,比如有一个人类的对象可以看成普遍 ...
- HDU 5877 Weak Pair(弱点对)
HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Jav ...
- eclipse svn 忽略 target目录 等等... 我用的后边的方法 (转载)
这个build失败的解决方案就是不要把你项目的 target目录放在src repository 里面,还有 .project 和 .classpath 最好也别放到src repository 里. ...
- [转载] Codis作者黄东旭细说分布式Redis架构设计和踩过的那些坑们
原文: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=208733458&idx=1&sn=691bfde670fb ...
- HDU5807分段dp
DAG图. [题意] n(50)个城市m(c(n,2))条单向边(x,y),保证x<y 对于三个点(x,y,z)如果abs(w[x]-w[y])<=K && abs(w[x ...
- JavaWeb学习总结(一)—JavaWeb开发入门及环境搭建
一.基本概念 1.1.软件体系结构 1.C/S:Client/Servlet,例如QQ就是CS结构需要编写服务器端程序和客户端程序.缺点:更新需要两端,总要求客户下载新的客户端程序优点:安全性比较好2 ...
- VisualSVN官网
VisualSVN是一款图形化svn服务器. http://www.visualsvn.com/
- vitamio框架
import io.vov.vitamio.LibsChecker; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.MediaPla ...
- ElasticSearch(ES)和solr的关系和区别
可以参考这篇文章:http://www.cnblogs.com/chowmin/articles/4629220.html Solr 2004年诞生(当时是Solar). ElasticSearch ...
- Js_字符串操作
字符串操作方法 charAt()以单字符串的形式返回给定位置的哪个字符串 例: var num = "hello world"; alert(num.charAt(1))//“e” ...