正常的没想到的DP和玄学贪心。

题目描述

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 \(A_1, ... , A_N (1 \le N \le 2,000)\) describing the elevation \( (0 \le A_i \le 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 \(B_1, ... , B_N\) 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|+...+|A_N-B_N|\)

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: \(A_i\)

输出格式:

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

输入输出样例

输入样例#1:

7
1
3
2
4
5
3
9
输出样例#1:

3

题解 of DP:

    首先这个题有一个比较正常的DP做法,但是长时间的思维定式会让人不敢朝这里想。

    看到\(2,000\)的数据范围,可以联想到的复杂度是\(O(n^2),O(n^2\log n)\)等,因此考虑DP或递推。状态如果从前面所有段转移过来,感觉不太现实,而且不容易处理。不过我们手玩发现,把一段路的高度改为没有修改过的是最优的一种方案。因此把一开始的数据离散化,得到不超过\(n\)个数。

    考虑DP,f[i][j]表示到第i段路满足不下降(只做不下降,不上升待会再做一遍),且当前高度为j所需要的最小花费。它的转移实际上是\(\min\limits_{k\in[1,j]}{f[i-1][k]}+|h[i]-ori[j]|\),ori[j]是j对应的离散化之前的数,h[i]是i原来的高度,因为不下降,所以只能从\([1,j]\)转移。不过这样的复杂度是\(O(n^3)\)的,我们发现,随着\(j\)的增大,\(k\)的取值范围只是上界变大了,因此可以通过更新前缀和来维护\(minn[i]=\min\limits_{j=[1,i]}\{ f[j]\}\)。

    同时,因为循环\(1-n\)在最外层,所以那层可以滚掉,同时f数组就可以直接更新前缀和,因此也不用额外开一个数组了。

Code of DP:

#include<cstdio>
#include<cstring>
#include<algorithm>
long long Abs(long long x)
{
return x>0?x:(-x);
}
long long Min(long long x,long long y)
{
return x<y?x:y;
}
struct node
{
long long a,b,i;
friend bool operator <(node x,node y)
{
return x.i<y.i;
}
}a[2001];
bool cmp(node x,node y)
{
return x.a<y.a;
}
long long ori[2001];
long long f[2001];//f[i]代表f[i]到f[n]的最大值
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i].a);
a[i].i=i;
} std::sort(a+1,a+n+1,cmp);
int cnt=0;
a[0].a=-1;
for(int i=1;i<=n;++i)
{
if(a[i].a!=a[i-1].a)
{
++cnt;
ori[cnt]=a[i].a;
}
a[i].b=cnt;
}
std::sort(a+1,a+1+n); long long ans=1000000000000000ll; memset(f,0,sizeof(f));
f[cnt+1]=10000000000000ll;
for(int i=n;i;--i)
{
for(int j=1;j<=cnt;++j)
f[j]+=Abs(a[i].a-ori[j]);
for(int j=cnt;j;--j)
f[j]=Min(f[j],f[j+1]);
}
for(int i=1;i<=cnt;++i)
ans=ans<f[i]?ans:f[i];
printf("%lld\n",ans);
return 0;
}

题解 of 贪心:

    在翻最优解代码时,发现了一种玄学贪心,用了堆优化,时间复杂度是\(O(n\log n)\),分析了很久感觉算法是对的但是不知道是什么原理。后来@Dew推出来了应该是正确的思路。

    对于维护从左到右的不下降性质,可以用一个大根堆。假设当前做到第\(i\)段,前面的\(1-i-1\)段都已经保证了不下降性质,每一段的高度都被放在了堆里,那么如果现在插入了一个数不满足不下降性质,我们就从大根堆里取出堆顶\(x\)。我们知道,要使\(a_i\)和\(x\)这两个元素满足不下降,所要付出的代价至少是\(|a_i-x|\)。而我们可以发现,对于两个数,假设是5和2,我们付出\(|a_i-x|=3\)的代价可以使这两个数满足不下降这几种中的任意一种:\(\{5,5\},\{4,4\},\{3,3\},\{2,2\}\),由于贪心,我们把5取出来,把\(\{2,2\}\)放进去,并计算一个3的代价,这个代价是必须的。不过现在序列可能变成了\(\{1,3,4,2,2\}\),它好像不满足不下降了。

    不过我们心里要明白,最后的\(\{2,2\}\)是活动的,可以换成\(\{4,4\}\)或\(\{5,5\}\),\(\{2,2\}\)只是一个下界。而我们把5这个“障碍”移开了,如果接下来再出现小于4的,就可以把4的位置换成新来的元素,而不论怎样,因为他们可以被替换,所以还是维持了不下降的性质。而因为5被替换后,4成了最大的,所以那些活动的元素上界就成了4,此时满足以4结尾的不下降序列。

Code of greedy:(虽然都是ms级的

#include<cstdio>
#include<queue>
using std::priority_queue;
priority_queue<int> q;
priority_queue<int> q1;
int a[2010];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]); long long sum=0,ans=0;
for(int i=1;i<=n;++i)
{
q.push(a[i]);
if(q.top()>a[i])//a[i]不是最大的,就需要调整
{
sum+=q.top()-a[i];
q.pop();
q.push(a[i]);
}
}
ans=sum;
sum=0;
for(int i=n;i;--i)
{
q1.push(a[i]);
if(q1.top()>a[i])
{
sum+=q1.top()-a[i];
q1.pop();
q1.push(a[i]);
}
}
printf("%lld\n",ans<sum?ans:sum);
return 0;
}

【DP】+【贪心】【前缀和】洛谷P2893 [USACO08FEB]修路Making the Grade 题解的更多相关文章

  1. 洛谷 P2893 [USACO08FEB]修路Making the Grade 解题报告

    P2893 [USACO08FEB]修路Making the Grade 题目描述 A straight dirt road connects two fields on FJ's farm, but ...

  2. 洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心)

    洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/132 ...

  3. [USACO08FEB]修路Making the Grade

    [USACO08FEB]修路Making the Grade比较难的dp,比赛时打的找LIS,然后其他的尽可能靠近,40分.先举个例子61 2 3 1 4 561 2 3 3 4 5第4个1要么改成3 ...

  4. P2893 [USACO08FEB]修路

    直入主题. 农夫约翰想改造一条路,原来的路的每一段海拔是Ai,修理后是Bi花费|A_i–B_i|.我们要求修好的路是单调不升或者单调不降的.求最小花费. 数据范围:n<=2000,0≤ Ai ≤ ...

  5. 洛谷P3387 【模板】缩点 题解

    背景 今天\(loj\)挂了,于是就有了闲情雅致来刷\(luogu\) 题面 洛谷P3387 [模板]缩点传送门 题意 给定一个\(n\)个点\(m\)条边有向图,每个点有一个权值,求一条路径,使路径 ...

  6. BZOJ3675 & 洛谷3648 & UOJ104:[Apio2014]序列分割——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3675 https://www.luogu.org/problemnew/show/P3648 ht ...

  7. 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling 题解

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...

  8. [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)

    [NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...

  9. [洛谷P1029]最大公约数与最小公倍数问题 题解(辗转相除法求GCD)

    [洛谷P1029]最大公约数与最小公倍数问题 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P, ...

随机推荐

  1. 使用Dom4j操作XML数据

    --------------siwuxie095                             dom4j 是一个非常优秀的 Java XML 的 API, 用来读写 XML 文件 和操作 ...

  2. Build Path

    ------------siwuxie095 什么是 Build Path? 为什么使用 Build Path? 如: (1)创建一个Java工程:LearnBuildPath (2)点击 Next, ...

  3. javascript使用技巧总结,不断更新...

    1.使用a标签来获得当前页面相对地址的绝对地址 function getAbsoluteUrl(url){ var a; if(!a) a = document.createElement('a'); ...

  4. jmap, jhat命令

    jmap命令有下面几种常用的用法 jmap [pid] jmap -histo:live [pid] >a.log jmap -dump:live,format=b,file=xxx.xxx [ ...

  5. c# 常规验证基类

    using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions ...

  6. SpringMVC——概述

    Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一 Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架 Spring MV ...

  7. PC/APP/H5三端测试的相同与不同

    随着手机应用的不断状态,同一款产品的移动端应用市场占相较PC端也越来越大,那么app与PC端针对这些产品的测试有什么相同与不同之处呢?总结如下: 首先谈一谈相同之处: 一,针对同一个系统功能的测试,三 ...

  8. CORS同源策略

    同源策略以及跨域资源共享在大部分情况下针对的是Ajax请求.同源策略主要限制了通过XMLHttpRequest实现的Ajax请求,如果请求的是一个“异源”地址,浏览器将不允许读取返回的内容. 支持同源 ...

  9. [vscode] github travis 集成问题

    问题log $ npm install -npm ERR! 404 Not Found npm ERR! 404 npm ERR! 404 'types/mocha' is not in the np ...

  10. 使用VS Code编写Markdown文件

    VS Code默认支持Markdown文件文件格式,这里介绍两个比较实用的功能,后续有新发现,可以持续更新. 实时预览 顾名思义,实时编辑,实时预览解析效果. 在VS Code扩展中搜索"M ...