Also called WQS binary search or Lagrangian relaxation (IOI 2016 “Aliens”). If = best cost using exactly groups/operations is convex in , drop the dimension: charge a penalty per group, solve the unconstrained problem , and binary search so that the optimum uses groups. Then . Cost: (unconstrained solve) × .
// solve(lambda) -> {min over t of g(t) + lambda * t, the LARGEST t attaining it}
template<class S> ll aliens(int k, ll lo, ll hi, S solve) { // g convex, minimisation
ll ans = LLONG_MAX;
while (lo <= hi) {
ll mid = lo + (hi - lo) / 2;
auto [val, cnt] = solve(mid);
if (cnt >= k) ans = val - mid * k, lo = mid + 1; // penalty can still grow
else hi = mid - 1;
}
return ans;
}The search range must contain the slope , which is negative when adding groups makes things worse (penalty becomes a reward); with integer costs, integer suffices. For maximisation of a concave , subtract the penalty instead (or negate everything).
Proof: why the largest feasible gives
minimises iff is a subgradient of at , i.e. (convexity makes these intervals consecutive and nonincreasing in ). Let be the largest minimiser; it is nonincreasing in , and iff . So the binary search ends at (an integer when costs are integers), where itself is a minimiser because . Therefore . The answer is correct even if the solver reports some other at that , which is why ties must be broken consistently (largest here) but exactness is never required.
Recognising convexity
- Exactly segments of an array with a Monge segment cost (QI): is convex. This covers , equal-pair counts, 1D -medians/-means on a line.
- Exactly disjoint intervals / subarrays maximising total value: concave (min-cost-flow argument: each extra interval is an augmenting path of nonincreasing gain).
- Anything that is a min-cost flow with units of flow: the cost is convex in (successive shortest paths have nondecreasing lengths).
- Spanning tree with exactly special edges (parametric MST): add to every special edge’s weight and run Kruskal, binary searching until exactly special edges are picked; ties broken toward special edges (matroid exchange gives convexity) (Kruskal).
- When unsure, brute-force for small inputs and check .
Proof sketch: Monge segment costs give convex
Write over -paths from to in the DAG with edge weights . Take an optimal -path and -path . Walking both from left to right, there must be a position where “overtakes”: an edge of and an edge of with such that ‘s prefix up to and ‘s prefix up to use and edges respectively. Swap the tails: and both have exactly edges, and QI gives , so . Hence .
Example: exactly segments, cost
The unconstrained solve with penalty is (plus a segment counter), done with a monotone CHT in ; total . Track the count alongside the value and compare pairs so ties prefer more segments.
auto solve = [&](ll lambda) { // O(n^2) reference version of the inner DP
vector<pair<ll, int>> dp(n + 1, {LLONG_MAX, 0}); dp[0] = {0, 0};
for (int i = 1; i <= n; i++) for (int j = 0; j < i; j++) {
ll s = S[i] - S[j] + c; pair<ll, int> cand{dp[j].first + s * s + lambda, dp[j].second + 1};
if (cand.first < dp[i].first || (cand.first == dp[i].first && cand.second > dp[i].second)) dp[i] = cand;
}
return dp[n];
};
ll best = aliens(k, -(ll)4e12, (ll)4e12, solve); // slopes can be negative: more segments may cost moreNotes
- At most : if the unconstrained optimum () already uses groups, that’s the answer.
- Nested Aliens (two “exactly” constraints) works when the 2D function is jointly convex (L♮-convex); rare.
- Recovering a solution with exactly groups at the final : the solver’s solution may use another count with the same penalised cost; adjust by exchanging segments along the tie (problem-specific), or reconstruct from a DP that tracks both min and max counts.
- Aliens is the only standard technique whose complexity doesn’t depend on ; for small , divide and conquer optimisation is simpler (D&C Optimization).
- The famous instance of “at most non-overlapping intervals maximising value” is the stock-trading-with-at-most--transactions problem (Classic DP), where up to makes the plain DP infeasible and forces exactly this trick.