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,只能求 ...
随机推荐
- debian attempt to kill init!
之前想在debian下安装Oracle 11g,结果在安装之前要安装好多的依赖包. 在centos和redhat下可以很方便的使用yum来安装,哗啦啦一下全装完了, 后来我在debian下用apt-g ...
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- Visual C#两分钟搭建BHO IE钩子(转)
摘自:http://www.cnblogs.com/mvc2014/p/3776054.html 微软在1997年正式推出Browser Helper Object (BHO), 使程序员能够更好的对 ...
- Log4j XML 配置
Xml代码 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configurat ...
- V8 引擎的sort算法
用的是快速排序,有点小问题 function ArraySort(comparefn) { // In-place QuickSort algorithm. // For short (length ...
- zoj2589Circles(平面图的欧拉定理)
链接 连通图中: 设一个平面图形的顶点数为n,划分区域数为r,一笔画笔数为也就是边数m,则有: n+r-m=2 那么不算外面的那个大区域的话 就可以写为 n+r-m = 1 那么这个题就可以依次求出每 ...
- 关于js的兼容问题(小办法)!
今天整理了一下浏览器对JS的兼容问题,希望能给你们带来帮助,我没想到的地方请留言给我,我再加上: 常遇到的关于浏览器的宽高问题: //以下均可console.log()实验 var winW=docu ...
- heaters
https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...
- jQuery动态加载css文件实现方法
$("<link>").attr({ rel: "stylesheet",type: "text/css",href: &quo ...
- jQuery动态加载脚本 $.getScript();
jQuery.getScript("/path/to/myscript.js", function(data, status, jqxhr) { /* ...