还是挺难的一个题,看了书上的解析以后还是不会写,后来翻了代码仓库,发现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. linux 批量kill进程

    ps -ef | grep module- | grep -v module-mxm | cut -c 9-15 | xargs kill -9 ps -ef | grep module- 查找关键字 ...

  2. Cannot find `aapt.exe`. Please install the Android SDK Build-tools package

    Google has updated their SDK tools ("Android SDK Tools" Rev. 23) in a way that also requir ...

  3. mongodb更新操作

    除了查询条件,还可以使用修改器对文档进行更新. 1. $inc > db.tianyc03.find() { "_id" : ObjectId("50ea6b6f1 ...

  4. MySQL 5.5安装记录

    安装gnake ./configure && gmake && gmake install 安装ncurses-devel yum install -y ncurses ...

  5. October 17th 2016 Week 43rd Monday

    You only live once, but if you do it right, once is enough. 人生只有一次,但如果活对了,一次也就够了. Whether you do it ...

  6. js未定义判断

    if (typeof(homeType) == 'undefined') { //..... //..... } typeof函数判断,如果未定义的就会返回undefined,注意undefined ...

  7. winform 多个label绑定一个事件

    1当多个label帮到到一个事件后 private void jiandao_Click(object sender, EventArgs e) { //sender显示的是窗体上接受事件的label ...

  8. delphi.memory.分配及释放---New/Dispose, GetMem/FreeMem及其它函数的区别与相同

    我估摸着内存分配+释放是个基础函数,有些人可能没注意此类函数或细究,但我觉得还是弄明白的好. 介绍下面内存函数前,先说一下MM的一些过程,如不关心可忽略: TMemoryManager = recor ...

  9. FP-growth高效频繁项集发现

    FP-growth 算法优缺点: 优点:一般快于Apriori 缺点:实现比较困难,在某些数据上性能下降 适用数据类型:标称型数据 算法思想: FP-growth算法是用来解决频繁项集发现问题的,这个 ...

  10. HDU 5769 Substring 后缀数组

    Substring Problem Description ?? is practicing his program skill, and now he is given a string, he h ...