Making the Grade POJ - 3666
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
| A 1 - B 1| + | A 2 - B 2| + ... + | 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[i][j]表示dp到i这一位,最后一个数是j的最小花费,那么状态数是n*(l~r),显然是不行的。
考虑一个小优化,因为只有数j在序列中出现过才会有用,所以第二维可以改为第j大的数,这样子状态数就是n^2级别的了,转移是(上升)dp[i][j]=min(dp[i-1][1~j])+abs(hi[i]-hi[j)。
把这个式子列出来就知道怎么优化转移了,计D[i][j]=min(dp[i][1~j]),那么转移就是D[i][j]=min*+(D[i][j-1],dp[i][j]),转移就变成O(n)的了。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 2020
#define inf 0x3f3f3f3f3f
#define ll long long
using namespace std;
ll dp[MAXN][MAXN],last[MAXN],hi[MAXN],rak[MAXN],D[MAXN][MAXN],ans=inf;
int n; ll abss(ll x){
if(x<) return -x;
return x;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%lld",&hi[i]),rak[i]=hi[i];
sort(rak+,rak+n+);
memset(D,inf,sizeof(D));
memset(dp,inf,sizeof(dp));
for(int i=;i<=n;i++) dp[][i]=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(i==) dp[i][j]=abss(hi[i]-rak[j]);
else dp[i][j]=D[i-][j]+abss(hi[i]-rak[j]);
D[i][j]=min(D[i][j-],dp[i][j]);
}
for(int i=;i<=n;i++) ans=min(ans,dp[n][i]);
memset(dp,inf,sizeof(dp));
memset(D,inf,sizeof(D));
for(int i=;i<=n;i++) dp[][i]=;
for(int i=;i<=n;i++)
for(int j=n;j>=;j--){
if(i==) dp[i][j]=abss(hi[i]-rak[j]);
else dp[i][j]=D[i-][j]+abss(hi[i]-rak[j]);
D[i][j]=min(D[i][j+],dp[i][j]);
}
for(int i=;i<=n;i++) ans=min(ans,dp[n][i]);
printf("%lld\n",ans);
return ;
}
Making the Grade POJ - 3666的更多相关文章
- S - Making the Grade POJ - 3666 结论 将严格递减转化成非严格的
S - Making the Grade POJ - 3666 这个题目要求把一个给定的序列变成递增或者递减序列的最小代价. 这个是一个dp,对于这个dp的定义我觉得不是很好想,如果第一次碰到的话. ...
- A-Making the Grade(POJ 3666)
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4656 Accepted: 2206 ...
- 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,只能求 ...
随机推荐
- 基于SSM的在线考试系统
本系统功能非常完善,页面美观大方,技术新颖,选用主流数据库Mysql,表数量及结构适当,如果你需要做在线考试或者其它考试类系统,这个系统将非常有用. 其实,任何考试系统,无非试题不一样,所以如果你是做 ...
- mysql之explain详解
mysql之explain详解 mysql之explain各个字段的详细意思: 字段 含义 select_type 分为简单(simple)和复杂 type all : 即全表扫描 index : 按 ...
- 【ThinkPHP】API控制器中加入析构函数
<?php namespace app\api\controller; use think\Controller; class User extends Controller { public ...
- java对象排序(Comparable)详细实例
对象实现Comparable接口 public class Field implements Comparable<Field>{ private String name; private ...
- thinkphp6.0 composer 安装 web-token/jwt-framework 常见出错原因分析及解决方法
composer require web-token/jwt-framework 安装JWT出现错误提示 - web-token/jwt-framework v2.0.1 requires ext-g ...
- Day 2 Bash shell 认识
1.拍摄虚拟机的快照 2. 什么是Bash shell? 命令解释器,将用户输入的命令,翻译给内核程序,将用户输入的指令翻译给内核 程序,内核处理完成之后将结果返回给bash. 如何打开一个bash窗 ...
- iOS渠道追踪统计方法大全
说起 iOS 的渠道统计,不少人会想到苹果官方的 App 分析功能(iTunes Connect),但实际操作中我们会发现,这个服务的统计维度还不够全面,许多广告主和运营人员更关心的是各个推广渠道实际 ...
- MyBatis 3.5.2 新特性介绍
1.MyBatis 最新版本 3.5.2 发布 MyBatis最新版本是:3.5.2,发布时间是:2019年7月15日 2.MyBatis 3.5.2 新特征介绍 我们知道,MyBatis 是支持定制 ...
- Wordpress SEO
Wordpress SEO 安装插件 Baidu Sitemap Generator, 作者 柳城, 主要用于按照配置参数生成 sitemap.xml 网站地图. 设置路径 设置 => Baid ...
- 关于react-router最新版本的使用
现在react-router已经更新到了5.1.1版本,在一些使用方法上较之前有了很多改变,现做初步列举,以后会陆续更新. 关于引入react-router和基本使用 旧版本中引入react-rout ...