Binary Search on Arbitrary Array
Codeforces 935 E Binary Search: Binary search is a widely used and efficient search algorithm, predicated on the assumption that the given data is sorted (either in ascending or descending order). This problem presents an intriguing conclusion: for any permutation of numbers from 1 to n, allowing the swapping of two elements' positions, regardless of whether the permutation is sorted or not, we can still use binary search to find the target element. The question then becomes: how do we select the elements to swap?
First, let's introduce the binary search used in the problem. Suppose we have a permutation p and we want to find element x. Let l = 1 and r = n + 1 (assuming the original permutation is 1-indexed). We execute the following loop:
- If
r - l == 1, end the loop and returnp[l]. - Let
m = (l + r) // 2. - If
p[m] <= x, letl = m; otherwise, letr = m.
An intuitive idea is to perform binary search on the original permutation. Suppose the last obtained element is y, we swap x and y (if y == x, then no swapping is needed and we have found x). This algorithm seems plausible, but proving its correctness rigorously is not straightforward. Let's consider the sequence formed by each element p[m] obtained during the search process. There are several scenarios to consider:
- If
xdoes not appear in this sequence: swappingxandydoes not seem to affect the entire search process, so we can eventually findx. However, we need to consider two cases to prove this rigorously.- If
yappears only once, swappingxandywill not affect the search process before the final step, so after the swap, we can obtainx. - If
yappears multiple times, we need to replace all occurrences ofywithx. Will this affect our search process? The answer is no. According to the definition of the problem, if we can provey <= x, replacingywithxwill not affect the search process. And precisely becauseyappears multiple times, there must bey <= x; otherwise,ywould become the right endpointrand would not appear more than once in the sequence ofp[m].
- If
- If
xappears in this sequence, we also need to provey <= x. Here, we can prove a more general conclusion: if at some point during the search,p[l] <= x, then the final output element must be<= x. We can prove this by mathematical induction on the value ofr - l. Ifr - l == 1, the conclusion holds. Ifr - l > 1, consider the nextp[m]. Ifp[m] <= x, then letl = m, and by the induction hypothesis, the conclusion holds; ifp[m] > x, then letr = m, and again, by the induction hypothesis, the conclusion holds.
In this way, we have completed the rigorous proof of the correctness of this algorithm. In fact, through the proof, we find that this algorithm is not limited to permutations from 1 to n; it holds true for any sequence.