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).

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 .

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 more

Notes

  • 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.