Dynamic Programming21 sections · 916 units
Open in Course

IOI Aliens - Implementation

High-level structure

function solve(points, k)
    preprocess points (sort, remove dominated)
    binary search on lambda
    for each lambda, compute dp with CHT
    count photos used
    adjust search range
    return optimal cost - k * lambda

function dpWithCHT(lambda)
    hull := empty convex hull structure
    for i from 1 to n
        dp[i] := query hull at position x[i] + lambda
        add line for point i to hull
    return (dp[n], count)

The CHT handles the quadratic cost function. Combined with Aliens Trick. Time: O(nlognlogC)O(n \log n \log C), Space: O(n)O(n).