P2893 [USACO08FEB]修路Making the Grade

题目描述

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. 农夫约翰想改造一条路,原来的路的每一段海拔是A_i,修理后是B_i,花费|A_i – B_i|。我们要求修好的路是单调不升或者单调不降的。求最小花费。

输入输出格式

输入格式:

  • Line 1: A single integer: N

  • Lines 2..N+1: Line i+1 contains a single integer elevation: Ai

输出格式:

  • 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.

这个题,从各种意义上讲都不难,然而我没想出来没想出来。。。

首先一定是要离散化的,因为不会去修一段没有出现的高度(可以证明这样不比最优解好)

设离散化以后的坐标,\(b[i]\)代表离散化以后排名为\(i\)的数字的值

\(dp[i][j]\)代表前\(i\)个组成的合法序列末尾元素的排名为\(j\)的最小花费

则有转移(单调不降)

\(dp[i][j]=min_{k=1}^j(dp[i][k]+abs(a[i]-b[j]))\)

显然可以前缀和优化一下子

复杂度\(O(N^2)\)


Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
const int N=2e3+10;
int dp[N][N],g[N][N],n,a[N],b[N];
int min(int x,int y){return x<y?x:y;}
int max(int x,int y){return x>y?x:y;}
int abs(int x){return x>0?x:-x;}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
std::sort(b+1,b+1+n);
memset(g,0x3f,sizeof(g));
memset(g[0],0,sizeof(g[0]));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
dp[i][j]=g[i-1][j]+abs(a[i]-b[j]);
g[i][j]=min(g[i][j-1],dp[i][j]);
}
int ans=g[n][n];
memset(g,0x3f,sizeof(g));
memset(g[0],0,sizeof(g[0]));
for(int i=1;i<=n;i++)
for(int j=n;j;j--)
{
dp[i][j]=g[i-1][j]+abs(a[i]-b[j]);
g[i][j]=min(g[i][j+1],dp[i][j]);
}
ans=min(ans,g[n][n]);
printf("%d\n",ans);
return 0;
}

事实上有一种更加神奇的做法

先说操作(单调非降的):

从左到右,把当前位置的数放进大根堆,然后比较这个数和堆顶的大小。

若比堆顶大,就不管

若比堆顶小,就把堆顶拿出来变成这个数,然后答案增加堆顶与这个数的差

代码大概是这样

for(int i=1;i<=n;i++)
{
scanf("%d",&a);
q.push(a);
if(a<q.top())
{
ans+=q.top()-a;
q.pop();
q.push(a);
}
}

为什么捏?

假设塞到第\(i\)了,前面是一个合法的递增序列,堆顶为\(y\),当前为\(x\)且\(x<y\)

这时候我们花掉了\(y-x\)块钱进行调整,考虑我们调整可以得到哪些结果

二元组\((x,x),(x+1,x+1),..(y-1,y-1),(y,y)\)都是可能的结果,虽然有的结果可能不合法,但一定存在合法的结果

我们尽可能想让当前的数值小,所以我们尽可能会选择小的合法结果

这时候我们发现,如果堆顶在后面被更新了,我们的合法结果的选择集合就变了

如果我们直接把最小的可能不合法的结果放进堆,那么当比它大的元素都被砍掉后(也就是它成了堆顶),它就变得合法了


2018.8.28

洛谷 P2893 [USACO08FEB]修路Making the Grade 解题报告的更多相关文章

  1. 【DP】+【贪心】【前缀和】洛谷P2893 [USACO08FEB]修路Making the Grade 题解

        正常的没想到的DP和玄学贪心. 题目描述 A straight dirt road connects two fields on FJ's farm, but it changes eleva ...

  2. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...

  3. 【洛谷】CYJian的水题大赛 解题报告

    点此进入比赛 \(T1\):八百标兵奔北坡 这应该是一道较水的送分题吧. 理论上来说,正解应该是DP.但是,.前缀和优化暴力就能过. 放上我比赛时打的暴力代码吧(\(hl666\)大佬说这种做法的均摊 ...

  4. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  5. 洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX 解题报告

    P1337 [JSOI2004]平衡点 / 吊打XXX 题目描述 有 \(n\) 个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.\(X\)处就是公共的绳结.假设 ...

  6. 洛谷 P4151 [WC2011]最大XOR和路径 解题报告

    P4151 [WC2011]最大XOR和路径 题意 求无向带权图的最大异或路径 范围 思路还是很厉害的,上午想了好一会儿都不知道怎么做 先随便求出一颗生成树,然后每条返祖边都可以出现一个环,从的路径上 ...

  7. 洛谷 P3258 [JLOI2014]松鼠的新家 解题报告

    P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...

  8. 洛谷 P2420 让我们异或吧 解题报告

    P2420 让我们异或吧 题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中-xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B ...

  9. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

随机推荐

  1. php 使用当前时间点进行时间范围查询

    /** * 判断是否是吃早饭时间 */ $nowtime = time(); $start = strtotime('8:30:00'); $end = strtotime('9:30:00'); i ...

  2. vuls安装记录

    第一步安装go环境apt-get install golang-go(显示出错,go版本apt安装太低,apt-get purge golang-go卸载后手动安装,必须1.8.3以上) 还需将/us ...

  3. re模块(详解正则)

    re模块 imort re 1.\w \W print(re.findall('\w','ab 12\+- _*&')) #\w 匹配字母 数字 及下划线 执行结果:['a', 'b', '1 ...

  4. C语言实现二分查找

    二分查找优势:比顺序查找更有效率       特点:元素按顺序排列 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include ...

  5. ubuntu配置机器学习环境(三) opencv 安装

    这里使用脚本安装 一些教程里使用cmake 安装,很容易出错的 使用github上的安装脚本,自动化安装 参考链接 Ubuntu $ cd Ubuntu/2.4 $ chmod +x * # 如果要安 ...

  6. PHP.39-扩展-锁机制解决并发-MySQL锁、PHP文件锁

    锁机制适用于高并发场景:高并发订单.秒杀…… apache压力测试 Mysql锁详解 语法 加锁:LOCK TABLE 表名1 READ|WRITE, 表名2 READ|WRITE ......... ...

  7. 新版IdFTP解决中文乱码问题

    用XE10后开发FTP客户端,发现有中文乱码问题.这里也主要是编码的问题,在connect链接后,需要设置编码方可. 注意:  IndyTextEncoding_OSDefault;   该代码可能需 ...

  8. cloudera manager服务迁移(scm数据库在postgresql上,其他amon,rman,oozie,metastore等在mysql上)

    公司线上大数据集群,之前用的是公有云主机,现在换成了自己idc机房机器,需要服务迁移,已下为测试: 1.备份原postgresql数据库: pg_dump -U scm scm > scm.sq ...

  9. 用mapreduce读取hdfs数据到hbase上

    hdfs数据到hbase过程 将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 hbase先创建好表   cre ...

  10. 关于 poorpool

    poorpool 真名 chenyixiao.是一条傻逼题都不会做的没有脑子的咸鱼. 山西省临汾第一中学 高二 sx省队里最菜的那一个进队靠暴力. 普通的理科生,曾经爱好数学,然而到了高中发现自己所谓 ...