Skip to content

Commit ca57817

Browse files
committed
add links
1 parent befc147 commit ca57817

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

copypasta/dp.go

+22-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ LC70 爬楼梯 https://leetcode.cn/problems/climbing-stairs/
2828
- 有障碍物 https://atcoder.jp/contests/abc129/tasks/abc129_c
2929
LC198 打家劫舍 https://leetcode.cn/problems/house-robber/
3030
- 值域打家劫舍 LC740 https://leetcode.cn/problems/delete-and-earn/
31-
- 分组+值域打家劫舍 https://atcoder.jp/contests/abc403/tasks/abc403_d
31+
- 分组+值域打家劫舍 https://atcoder.jp/contests/abc403/tasks/abc403_d
32+
- 类似思路的题目 https://leetcode.cn/problems/the-number-of-beautiful-subsets/
3233
- 恰好选 floor(n/2) 个 https://atcoder.jp/contests/abc162/tasks/abc162_f
3334
- 矩阵打家劫舍 https://codeforces.com/problemset/problem/1195/C
3435
- 环形 LC213 https://leetcode.cn/problems/house-robber-ii/
@@ -3244,13 +3245,13 @@ func _(abs func(int) int) {
32443245
https://www.luogu.com.cn/problem/P1613
32453246
https://www.acwing.com/problem/content/296/ 计算重复
32463247
*/
3247-
binaryLifting := func(left []int, qs []struct{ l, r int }) []int {
3248+
binaryLifting := func(left []int) {
32483249
// 以 https://atcoder.jp/contests/arc060/tasks/arc060_c + https://leetcode.cn/problems/path-existence-queries-in-a-graph-ii/ 为例
32493250
n := len(left)
32503251
const mx = 17 // bits.Len(uint(n))
32513252
pa := make([][mx]int, n)
32523253
for i, l := range left {
3253-
pa[i][0] = l
3254+
pa[i][0] = l // 从 i 往左,最远可以跳到 l
32543255
}
32553256
for i := range mx - 1 {
32563257
for x := range pa {
@@ -3259,33 +3260,38 @@ func _(abs func(int) int) {
32593260
}
32603261
}
32613262

3262-
ans := make([]int, len(qs))
3263-
for qi, q := range qs {
3264-
l, r := q.l, q.r
3263+
// 从 r 跳到 <= l 的位置,最少要跳多少步
3264+
minJumps := func(l, r int) (res int) {
32653265
if l == r {
3266-
// ans[qi] = 0
3267-
continue
3266+
return
32683267
}
32693268
if l > r {
32703269
l, r = r, l
32713270
}
3272-
res := 0
32733271
for k := mx - 1; k >= 0; k-- {
32743272
p := pa[r][k]
32753273
if p > l {
3276-
res |= 1 << k
32773274
r = p
3275+
res |= 1 << k
32783276
}
32793277
}
3280-
res++
32813278
r = pa[r][0]
3282-
if r <= l {
3283-
ans[qi] = res
3284-
} else {
3285-
ans[qi] = -1
3279+
res++
3280+
if r > l {
3281+
return -1
32863282
}
3283+
return
32873284
}
3288-
return ans
3285+
3286+
// 从 i 开始,往左跳 k 步的位置
3287+
jumpK := func(i, k int) int {
3288+
for ; k > 0; k &= k - 1 {
3289+
i = pa[i][bits.TrailingZeros(uint(k))]
3290+
}
3291+
return i
3292+
}
3293+
3294+
_ = []any{minJumps, jumpK}
32893295
}
32903296

32913297
/* 数据结构优化 DP

0 commit comments

Comments
 (0)