Monster Game II¶
- Title:
Monster Game II - Judge / source:
CSES - Original URL: https://cses.fi/problemset/task/2085
- Secondary topics:
Li Chao tree,Affine DP,Line container - Difficulty:
hard - Subtype:
Online affine-DP optimization with arbitrary line/query order - Status:
solved - Solution file: monstergame2.cpp
Why Practice This¶
This is the cleanest in-repo anchor for the exact Li Chao route inside the CHT / Li Chao family.
The problem gives a recurrence family where:
- every previously killed monster offers one affine candidate
- the current monster contributes only the query point
s_i - the order constraints stay online
So the lesson is not "some random advanced trick."
It is:
- derive one line from each previous state
- keep an exact online minimum structure
- know why
Monster Game Iis lighter, butMonster Game IIwants the generic route
Recognition Cue¶
Reach for CHT / Li Chao when:
- the recurrence becomes
min_j(m_j x_i + b_j) - each previous choice contributes one line
- the current item only contributes the query point
- query order or slope order is not monotone enough for a deque hull
The strongest smell here is:
- "minimum cost after any previous checkpoint," where the algebra isolates one product
f_j * s_i
Problem-Specific Transformation¶
Let dp[i] be the minimum total time after killing monster i.
If the last previously killed monster is j, then the current kill cost is:
So state j contributes the line:
and current monster i asks for that line minimum at:
The initial skill factor x is just the first line:
where x' is the query coordinate.
So the problem becomes:
- insert initial line
(m = x, b = 0) - for each monster in order:
- query at
s_i - that answer is
dp[i] - insert new line
(m = f_i, b = dp[i])
Core Idea¶
Use a Li Chao tree for minimum queries over the domain of monster strengths.
Each node owns an x interval and stores the line that is currently best at that interval midpoint.
When a new line arrives:
- compare it against the stored line at the midpoint
- keep the midpoint winner in the node
- recurse with the loser only into the side where it can still become better
That keeps insertion logarithmic.
For each monster:
- query the minimum value at
s_i - store it as
dp[i] - insert the line generated by
f_i
This is exact, online, and does not need monotone slope/query order.
Complexity¶
If S is the integer strength domain width, then:
- each query:
O(log S) - each line insertion:
O(log S) - total:
O(n log S)
For CSES:
n <= 2 * 10^5s_i, f_i <= 10^6
so the solution fits comfortably.
Pitfalls / Judge Notes¶
- Use
long longfordp, but evaluatem * x + bwith__int128inside the line container. - Insert the initial line from the starting skill factor before processing any monster.
- This lane is minimum, not maximum.
Monster Game Ihas monotonicity that can support a lighter hull;Monster Game IIremoves that comfort.- Use the min/max strength range as the Li Chao domain, or another domain that definitely covers every query.
Reusable Pattern¶
- Topic page: Convex Hull Trick / Li Chao Tree
- Practice ladder: CHT / Li Chao ladder
- Starter template:
li-chao-tree.cpp - Notebook refresher: CHT / Li Chao hot sheet
- Compare points: Line Add Get Min, Monster Game I, DP Cheatsheet
- This note adds: the exact generic online route where the affine DP no longer depends on monotone order.
Solutions¶
- Code: monstergame2.cpp