Lagrangian Relaxation / Aliens Trick Hot Sheet¶
Use this page when the original problem asks for the best answer with exactly K choices, but the same problem becomes much cheaper if each choice costs a penalty lambda.
Do Not Use When¶
- plain
O(NK)DP already fits - the expensive part is still one DP row with monotone argmins -> Divide and Conquer DP hot sheet
- the state is one convex function over coordinate/value -> Slope Trick hot sheet
Exact Maximization Route¶
For fixed integer penalty lambda, solve:
\[
\max(\text{original value} - \lambda \cdot \text{count})
\]
and return:
penalized_valuecount
For equal penalized values, prefer the larger count.
Then:
- binary search the largest
lambdawithcount >= K - recover:
answer = penalized_value + lambda * K
Recognition Cues¶
- "choose exactly
Kintervals / segments / facilities / positions" - the count dimension is the only thing making the DP too expensive
- editorial mentions
Alien DP,wqs binary search,concavity, orLagrangian relaxation
Core Invariants¶
- the relaxed solver must return both value and count
- count must move monotonically as
lambdachanges - tie-break direction is part of correctness, not style
- integer slopes want integer binary search
Main Traps¶
- tie-breaking equal values by smaller count in a maximization route
- using floating-point search when the slopes are integral
- forgetting to add
lambda * Kback - assuming a penalty with exact count
Kalways exists
Exact Starter In This Repo¶
- starter ->
aliens-trick-nonadjacent-max.cpp - flagship rep -> Red and Blue Lamps
- compare direct optimization lanes -> Divide and Conquer DP hot sheet, Knuth Optimization hot sheet
Reopen Paths¶
- full tutorial -> Lagrangian Relaxation / Aliens Trick
- parent router -> DP cheatsheet
- compare coordinate-function family -> Slope Trick hot sheet