Slope Trick Hot Sheet¶
Use this page when your DP state is one convex piecewise-linear function f(x), and each transition only:
- adds
max(0, x-a) - adds
max(0, a-x) - adds
|x-a| - or widens where the minimizer may move
Do Not Use When¶
- the recurrence is really
min_j(m_j x + b_j)over previous states -> use CHT / Li Chao hot sheet - you only add
|x-a| + band never need a movement-styleshift_min; a lighter median-maintenance route may be enough - the DP state is not one convex function over a single coordinate
Core Operations¶
add_x_minus_a(a)-> addmax(0, x-a)add_a_minus_x(a)-> addmax(0, a-x)add_abs(a)-> add|x-a|shift_min(dl, dr)-> every minimizerxmay move to[x+dl, x+dr]expand_argmin_by(d)-> shorthand forshift_min(-d, d)seed_argmin_point(a, copies)-> contest helper for start states likef(a)=0,f(x)=+\inftyelsewhere
Core Invariants¶
min_fstores the current minimum value of the function- left max-heap stores left-side breakpoints
- right min-heap stores right-side breakpoints
- lazy offsets shift all breakpoints without touching every heap entry
- current minimizer interval is
[top_left(), top_right()]after offsets
Recognition Cues¶
- "minimum cost if you end at coordinate
x" - movement between steps becomes
min_{|y-x|<=d} f(y) - each event adds one one-sided damage or penalty
- editorial says the function stays convex and piecewise linear
Main Traps¶
- swapping the meanings of
add_x_minus_aandadd_a_minus_x - forgetting that bounded movement by distance
dmeansshift_min(-d, d) - trying to reconstruct full
f(x)from this starter - using slope trick for an affine family that should be CHT / Li Chao
Exact Starter In This Repo¶
- starter ->
slope-trick-basic.cpp - flagship rep -> Snuketoon
- warm-up compare point -> AtCoder ABC127 F - Absolute Minima
Reopen Paths¶
- full tutorial -> Slope Trick
- parent router -> DP cheatsheet
- compare affine family -> CHT / Li Chao hot sheet