还是挺难的一个题,看了书上的解析以后还是不会写,后来翻了代码仓库,发现lrj又用了一些玄学的优化技巧。

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std; typedef long long LL; const int maxn = 100 + 5;
const int maxx = maxn * maxn * 2;
const LL INF = (1LL << 60); LL h[maxn], x[maxx], dp[2][maxx]; int main() {
int T;
cin >> T;
while (T--) {
int n;
LL d;
cin >> n >> d;
for (int i = 0; i < n; i++)
cin >> h[i];
if (abs(h[0] - h[n - 1]) > (n - 1) * d) { //-specially judge impossible
cout << "impossible\n";
continue;
} // useful heights //-?
int nx = 0;
for (int i = 0; i < n; i++)
for (int j = -n + 1; j <= n - 1; j++)
x[nx++] = h[i] + j * d;
sort(x, x + nx);
nx = unique(x, x + nx) - x; // dp
int t = 0;
for (int i = 0; i < nx; i++) {
dp[0][i] = INF;
if (x[i] == h[0])
dp[0][i] = 0;
}
for (int i = 1; i < n; i++) {
int k = 0;
for (int j = 0; j < nx; j++) {
while (k < nx && x[k] < x[j] - d)
k++;
while (k + 1 < nx && x[k + 1] <= x[j] + d && dp[t][k + 1] <= dp[t][k])
k++; // min in sliding window
if (dp[t][k] == INF)
dp[t ^ 1][j] = INF; // (t, k) is not reachable
else
dp[t ^ 1][j] = dp[t][k] + abs(x[j] - h[i]);
}
t ^= 1;
}
for (int i = 0; i < nx; i++)
if (x[i] == h[n - 1])
cout << dp[t][i] << "\n";
}
return 0;
}

首先这是一个背包类型的问题,朴素算法不仅会tle,还会mle,所有我们可以分析问题,得到一个O(n2)的状态设计。

其次,为了方便枚举,lrj使用了一个数组存储了所有可能用到的更新状态,这是一种坐标离散化的技巧,不然数组会过大。

然后,为了节省枚举时所花费的复杂度,lrj使用了单调队列来优化转移,同时,考虑到f[i-1]先单调减,又单调加,不需要维护完整的队列,只需要维护最小值即可。

最后使用了滚动数组进行优化。

这是一道经典题。

[uva12170]Easy Climb的更多相关文章

  1. 【暑假】[深入动态规划]UVa 12170 Easy Climb

    UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路:  引别人一 ...

  2. Easy Climb UVA - 12170 滚动dp +离散化+ 单调队列优化

    E.Easy Climb Somewhere in the neighborhood we have a very nice mountain that gives a splendid view o ...

  3. Easy Climb

    题意: 有n块石头,给定他们的高度,现保持第一和最后一块高度不变,其他可增加和减少高度,求通过变换使所有相邻石头的高度差的绝对值不大于d,所变化高度总和的最小值. 分析: 状态还可以想出来,dp[i] ...

  4. Leetcode解题思路总结(Easy篇)

    终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有 ...

  5. Leetcode之70. Climbing Stairs Easy

    Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...

  6. 决战Leetcode: easy part(1-50)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

  7. leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)

    leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...

  8. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

  9. Struts2 easy UI插件

    一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tree([settings]); 常 ...

随机推荐

  1. CentOS7下安装和使用Xdebug

    wget http://xdebug.org/files/xdebug-2.4.0rc4.tgztar xvzf xdebug-2.4.0rc4.tgzcd xdebug-2.4.0RC4phpize ...

  2. 《linux命令》ps -aux详细解释

    本文转载自http://blog.chinaunix.net/uid-21516619-id-1824945.html 显示其他用户启动的进程(a) 查看系统中属于自己的进程(x) 启动这个进程的用户 ...

  3. Eclipse 的单步调试

    1.设置断点在程序里面放置一个断点,也就是双击需要放置断点的程序左边的栏目上. 2.调试(1)点击"打开透视图"按钮,选择调试透视图,则打开调试透视图界面,然后先设置断点,按调试按 ...

  4. CPrimerPlus第十一章中的“选择排序算法”学习

    C Primer Plus第十一章字符串排序程序11.25中,涉及到“选择排序算法”,这也是找工作笔试或面试可能会遇到的题目,下面谈谈自己的理解. 举个例子:对数组num[5]={3,5,2,1,4} ...

  5. 初学Redis(1)——认识Redis

    Redis官网对Redis的定义是:“Redis is an open source, BSD licensed, advanced key-value cache and store”,可以看出,R ...

  6. iOS之九宫格图片

    照片 现在人们的生活越来越丰富了,很多美好的瞬间都定格在一张张色彩绚丽的照片上,或许把照片珍藏在相册里,或许通过社交软件分享给亲朋好友.那社交软件上的照片是以什么形式展现的呢?那么接下来就要说到九宫格 ...

  7. Handlebars块级Helpers

    1.Handlebars简单介绍: Handlebars是JavaScript一个语义模板库,通过对view和data的分离来快速构建Web模板.它采用"Logic-less templat ...

  8. Power BI for Office 365(七) Power BI站点

    报表分享是Power BI for Office 365比较关键的一个功能,其中提供的Power BI站点将给这些报表一个安全的"归宿",同时也可以跟任何想要分享的人进行协作--- ...

  9. ExtJS 中自定义类

    首先我们来看一看在Javascript中,是怎样自定义类的: var Person = function (name, age) { this.Name = ""; this.Ag ...

  10. openfoam 的安装【转载】

    原文地址: http://blog.sina.com.cn/s/blog_14bf001d10102wifw.html OpenFOAM安装 OpenFOAM基于Linux系统下运行,由于对Linux ...