Golden Technique For You

Правка en1, от SirKadirov009, 2026-12-01 13:48:38

The Trick That Made Me Stop Fearing "Maximize the Minimum" Problems (Binary Search on the Answer)

Difficulty: beginner to intermediate. Reading time: about 6 minutes.

Binary search depiction
Binary search halving the search range at every step. Image: "Binary Search Depiction" by Wikimedia Commons (CC BY-SA).

Have you ever opened a problem and seen this?

"Maximize the minimum distance between any two cows."

You stare at it. You think about DP, greedy, sorting, and nothing clicks. Then someone in the comments writes "just binary search on the answer" and you feel like everyone knew a secret except you.

This post is that secret, explained so you can use it today.

The big idea

Normally we binary search in an array. Here we binary search on the answer itself.

Instead of asking "What is the best answer?" (hard), ask:

"Is answer X achievable?" (often easy!)

If the answer to that yes/no question is monotonic (all "yes" up to some point, then all "no", or vice versa), you can binary search over X.

X:      1  2  3  4  5  6  7  8  9
check:  Y  Y  Y  Y  Y  N  N  N  N
                    ^
              the best answer

Example: Aggressive Cows

You have

Unable to parse markup [type=CF_MATHJAX]

$ stalls at positions

Unable to parse markup [type=CF_MATHJAX]

$ and

Unable to parse markup [type=CF_MATHJAX]

$ cows. Place the cows so the minimum distance between any two cows is as large as possible.

Step 1: Flip the question. Can we place all

Unable to parse markup [type=CF_MATHJAX]

$ cows so every pair is at least

Unable to parse markup [type=CF_MATHJAX]

$ apart?

Step 2: Check greedily. Sort the stalls. Put the first cow in the first stall, then put each next cow in the first stall at least

Unable to parse markup [type=CF_MATHJAX]

$ away from the previous one. If we place

Unable to parse markup [type=CF_MATHJAX]

$ cows,

Unable to parse markup [type=CF_MATHJAX]

$ is feasible.

Step 3: Check monotonicity. If

Unable to parse markup [type=CF_MATHJAX]

$ works, then any smaller distance also works. So the feasible values form a clean prefix, which means we can binary search.
#include <bits/stdc++.h>
using namespace std;

int n, k;
vector<long long> a;

bool can(long long d) {
    int cnt = 1;
    long long last = a[0];
    for (int i = 1; i < n; i++) {
        if (a[i] — last >= d) {
            cnt++;
            last = a[i];
        }
    }
    return cnt >= k;
}

int main() {
    cin >> n >> k;
    a.resize(n);
    for (auto &x : a) cin >> x;
    sort(a.begin(), a.end());

    long long lo = 1, hi = a.back() — a.front(), ans = 0;
    while (lo <= hi) {
        long long mid = lo + (hi — lo) / 2;
        if (can(mid)) {
            ans = mid;
            lo = mid + 1;
        } else {
            hi = mid — 1;
        }
    }
    cout << ans << "\n";
}

Complexity:

Unable to parse markup [type=CF_MATHJAX]

$, where

Unable to parse markup [type=CF_MATHJAX]

$ is the maximum coordinate range. That is fast enough for

Unable to parse markup [type=CF_MATHJAX]

$ up to

Unable to parse markup [type=CF_MATHJAX]

$ or more.

The 3-question checklist

Before using this trick, ask yourself:

  1. Is the answer a number in a bounded range? (time, distance, size, cost...)
  2. Can I write a check(x) that returns yes/no?
  3. Is check monotonic? If x works, does x-1 (or x+1) also work?

If all three are yes, you've probably found a binary search problem.

Common mistakes (I've made all of them)

  • Infinite loops. Use mid = lo + (hi — lo) / 2 and be careful whether you move lo = mid + 1 or hi = mid.
  • Overflow. Upper bounds like

    Unable to parse markup [type=CF_MATHJAX]

    $ break int. Use long long.
  • Wrong bounds. If hi is too small, you silently get the wrong answer. When in doubt, make it bigger. It only costs a few extra iterations.
  • Non-monotonic checks. If your check isn't monotonic, binary search will lie to you. Test with small cases.

Practice problems

Final thoughts

The scariest-looking optimization problems often become easy once you stop asking "what's the best answer?" and start asking "is this answer possible?" That one shift in thinking took me from stuck to solving a whole new class of problems.

Your turn: what's the coolest problem you've solved with this trick? Drop it in the comments, and if this helped, an upvote helps others find it!

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский SirKadirov009 2026-12-01 13:48:38 5117 Initial revision (published)