POJ 3666 Making the Grade (线性dp,离散化)
Making the Grade
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
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.
<i>N</i><br>* Lines 2..<i>N</i>+1: Line
<i>i</i>+1 contains a single integer elevation:
<i>A<sub>i</sub></i> </p>
cost for FJ to grade his dirt road so it becomes nonincreasing or nondecreasing
in elevation.</p>
1
3
2
4
5
3
9
对于这个引理,简单地证明一下:
可以用数学归纳法证明。
假设b[1~k-1]都在a[]中出现过;
对于b[k],若a[k]>b[k-1],则b[k]=a[k]解最优;
若a[k]<b[k-1],则一定存在t,使b[t~k]变为a[k]的最优解;
或b[k]=b[k-1];
特殊地,对于1号位置,b[1]=a[1]必定为最优解。
综上,引理显然得证。
接下来进行dp

我们令dp[i][j]来表示前i个数中形成单调序列并且最后一个为j的 最小花费;那么因为最后一个数为j,所以之前的数必须小于j,所以(i,j)的花费 为min{dp[i][k]}+j-a[i];
所以状态转移方程为 dp[i][j]=min{dp[i][k]}+j-a[i];
还不知道怎么推
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 0x3f3f3f3f;
using namespace std;
int n;
int a[], num[], f[][];
int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
num[i] = a[i];
}
sort(num + , num + n + );
memset(f,0x3f3f3f3f, sizeof(f));
f[][] = ;
int tmp = ;
int i, j;
for (i = ; i <= n; i++)
{
tmp = f[i-][];
for (j = ; j <= n; j++)
{
tmp = min(tmp, f[i - ][j]);
f[i][j] = abs(num[j] - a[i]) + tmp;
}
}
int ans = inf;
for (i = ; i <= n; i++)
{
ans = min(ans, f[n][i]);
}
printf("%d\n", ans);
return ;
}
POJ 3666 Making the Grade (线性dp,离散化)的更多相关文章
- 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 ...
- Poj 3666 Making the Grade (排序+dp)
题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...
- poj3666/CF714E/hdu5256/BZOJ1367(???) Making the Grade[线性DP+离散化]
给个$n<=2000$长度数列,可以把每个数改为另一个数代价是两数之差的绝对值.求把它改为单调不增or不减序列最小代价. 话说这题其实是一个结论题..找到结论应该就很好做了呢. 手玩的时候就有感 ...
- POJ 3666 Making the Grade (DP)
题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...
- POJ 3666 Making the Grade (DP滚动数组)
题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...
- POJ 3666 Making the Grade【DP】
读题堪忧啊,敲完了才发现理解错了..理解题必须看样例啊!! 题目链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110495#pro ...
- POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)
传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
随机推荐
- sqlserver 2008评估期已过
sqlserver 评估期已过 分类: SQL SERVER2012-08-22 17:04 977人阅读 评论(0) 收藏 举报 打开sqlserver出现提示:评估期已过.有关如何升级的测试版软件 ...
- 在ant中将依赖jar包一并打包的方法
一般jar包里面是不包含jar文件的,如果自己的类有依赖其他jar包,可以通过ant命令将这些jar包解析,然后和自己的class文件打在一起,命令如下: build.xml 1 2 3 4 5 6 ...
- DevExpress v18.1新版亮点——WPF篇(五)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WPF v18.1 的新功能,快来下载试用新版本!点击下载& ...
- Goolge全球各国域名大全
搜索过技术文章的朋友都知道,Google的搜索功力绝对是世界第一,仅仅搜索中文还体现不出Google的功力,如果大家是做外贸或者搜索其它语言的文章,就会知道Google的内涵有多少了.全世界的国家Go ...
- 太完美 TWM000极度精简版XP20130123终结美化版
TWM000极度精简版XP20130123终结美化版:蛋蛋20130123终结版为蓝本,虫子提供的美化包进行了美化.此版经测试完美在Z77主板开启AHCI安装,此为最终版之美化版!LiteXPMH.i ...
- 深入理解uwsgi和gunicorn网络模型
前言: 去年10月份建了一个python技术群,到现在为止人数已经涨到700人了.最一开始我经常在群里回应大家的问题,不管是简单还是困难的,我都会根据自己的经验来交流. 让人新奇的是一些初学者关注最多 ...
- LARC Caffe笔记(二) 训练自己的img
继看完 贺完结!CS231n官方笔记 上一次已经成功跑起caffe自带的例程,mnist和cifar10 但是终归用的是里面写好的脚本,于是打算训练自己的img 〇.目标 准备好food图片3类(出于 ...
- JavaScript学习(二)——深入学习js对象的原型与继承
了解对象 什么是对象? …… 这个就不说了 对象的声明的两种方式 var person = new Object(); person.name="linchen"; pers ...
- 【转】游戏buff设计参见
其实这类帖子并没有多少的设计理论,对于策划的提升和帮助也并不大,原因其实在于其适用性太窄,当我要设计XX象棋的时候,它就滚一边去了. 废话不多说切入正题: 游戏中的BUFF/DEBUFF我们见过很多, ...
- caffe安装编译问题-ImportError: libopencv_core.so.3.4: cannot open shared object file: No such file or directory
问题描述 >>> import caffe Traceback (most recent call last): File , in <module> File , in ...