---
id: 83
lang: en
translation_of: index.md
source_sha256: 5e0bdf406353ef507eacc65ed98f0bbaeedab2552698bc8fcc0d0145e605f3f5
title: "P2678: Jumping Stones — Solution & the Principle of Binary Search on the Answer"
description: "A solution to Luogu P2678 (Jumping Stones) using binary search on the answer, plus a short summary of how the technique works."
tags: ["OI","Binary Search","Binary Search on the Answer","Enumeration"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "跳石头 (the problem name, NOIP 2015) rendered literally as “Jumping Stones”."
  - "NOIP提高组 rendered as “the NOIP Senior Division”."
  - "亦复如此 (closing phrase, “and likewise again”) rendered as “And so on, over and over.”"
---

This problem was handed out when I was training with the <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">godlike</span> elementary schoolers over the summer, and I never knew how to solve it. A month ago the <span class="slang" data-zh="大佬" data-gloss="dàlǎo, “big guy”. A highly skilled person, an expert; used with admiration.">big shot</span> crazydave gave me a quick rundown of how binary search on the answer works, and I, <span class="slang" data-zh="蒟蒻" data-gloss="jǔruò, “konjac”, a pun on 巨弱 (jù ruò), “very weak”. Self-deprecating word for oneself.">a weakling</span>, only got it all sorted out today.

Let’s look at the example problem first: Luogu P2678

This is an easy problem from the NOIP Senior Division (I’m way too <span class="slang" data-zh="菜" data-gloss="cài, “vegetable”. Slang for being bad at something; weak.">weak</span>). The main idea is to enumerate the answer and, at each step of the enumeration, just check whether it meets the requirements. Code first:

```cpp
// P2678.cpp
#include <iostream>

using namespace std;

const int maxn = 500020;
int L, N, M;
int D[maxn];

bool check(int num)
{
    int last = 0;
    int cnt = 0;
    for (int i = 0; i <= N; i++)
        if (D[i] - last < num)
            cnt++;
        else
            last = D[i];
    if (cnt > M)
        return false;
    return true;
}

void solve()
{
    int left = 0;
    int right = L;
    int ans = 0;
    while (left <= right)
    {
        int mid = (left + right) / 2;
        if (check(mid))
            left = mid + 1, ans = mid;
        else
            right = mid - 1;
    }
    cout << ans;
}

int main()
{
    cin >> L >> N >> M;
    for (int i = 0; i < N; i++)
        cin >> D[i];
    D[N] = L;
    solve();
    return 0;
}
```

The check function verifies whether the answer num (that is, the answer we enumerated) meets the requirements; here, that means checking whether, when the minimum distance is num, the stones removed exceed the limit. If they do, it returns false, and the binary search adjusts the interval to [left,mid-1], lowering the maximum to probe the limit; if they don’t, it returns true, and the binary search adjusts the interval to [mid+1,right], in pursuit of a larger maximum.

## Abstracted Analysis Results

Before you set out to enumerate the answer to a problem, consider binary search on the answer. If binary search on the answer works, the time complexity drops drastically, from O(n^2) to O(n log n). The structure of binary search on the answer is just the binary search code plus the code that checks whether an answer is correct. If an answer passes the correctness check, go for a larger (smaller) answer and enumerate again; if it fails, adjust the interval and go for a larger (smaller) answer within the smaller (larger) interval. And so on, over and over.
