Remove a “exactly groups” constraint from a DP by turning it into a penalty, then binary searching the penalty. Named after the IOI 2016 problem Aliens; also called Lagrangian relaxation or the WQS binary search.

The setup

You can solve

in or , but the problem demands exactly groups, and adding a dimension costs a factor of .

The idea

Let be the optimal cost using exactly groups. If is convex in , then:

  • Adding a penalty per group and minimising freely selects the where the marginal gain of another group equals .
  • Increasing makes the optimiser use fewer groups — monotonically.
  • So binary search until the unconstrained solve happens to use exactly groups.
  • Recover the answer as .

Geometrically: is the slope of a supporting line to the convex curve , and you are searching for the slope that touches it at .

The template

// solve(lambda) returns {cost + lambda * groups, groups_used}
pair<long long,int> solve(long long lambda);
 
long long aliens(int k, long long lo, long long hi) {
    long long best = 0;
    while (lo <= hi) {
        long long mid = lo + (hi - lo) / 2;
        auto [cost, cnt] = solve(mid);
        if (cnt >= k) { best = cost - mid * k; lo = mid + 1; }   // penalty too small
        else hi = mid - 1;
    }
    return best;
}

Search over integer when costs are integers — the optimum is attained at an integer slope, and integer search avoids all floating-point trouble.

The two requirements

  1. Convexity of . This is the real condition, and it must be argued or tested. Verify empirically: brute-force for small and check that the second differences are all .
  2. A fast unconstrained solver. Usually or with CHT or a monotone queue.

Total: .

The tie-breaking problem

The hardest part

At the optimal , several group counts often achieve the same penalised cost — so the solver may return even though is correct. Two standard fixes:

  1. Track a second criterion. Among equal costs, make the solver prefer the maximum (or minimum) group count, consistently. Then binary search on that count.
  2. Do not require exactness. Binary search for the largest with , and return . Convexity guarantees this is even when the solver never reports exactly .

The second is more robust and is what the template above does.

When to reach for it

Signal
”exactly segments / groups / operations”
is large (), so an DP is too slow
The unconstrained version is easy
The cost-vs- curve is convexrequired
is smalluse plain D&C DP — simpler

Typical problems

  • Partition an array into exactly segments minimising the sum of segment costs
  • Choose exactly non-overlapping intervals maximising total value
  • Buy and sell a stock at most times (the LeetCode classic, at )
  • Build exactly facilities minimising total distance
  • Select exactly edges of a spanning tree with a colour constraint (parametric MST — same idea, applied to Kruskal)

The parametric MST version is worth noting: to find a spanning tree with exactly white edges, add to every white edge’s weight and binary search until Kruskal picks exactly of them.

Relationship to other techniques

Keeps the dimensionTime
Plain DPyes
D&C DPyes
Aliens trickno or

Aliens is the only one whose complexity is independent of — which is exactly why it exists.

See also: D&C DP · Convex Hull Trick · Binary Search