2020-06-04 01:42:57 +00:00
|
|
|
---
|
2020-06-15 23:19:07 +00:00
|
|
|
id: binary-search
|
2020-06-04 14:28:47 +00:00
|
|
|
title: "Binary Search"
|
2020-06-05 23:04:47 +00:00
|
|
|
author: Nathan Chen
|
2020-06-05 00:37:02 +00:00
|
|
|
prerequisites:
|
|
|
|
-
|
|
|
|
- Silver - Sorting
|
2020-06-04 01:42:57 +00:00
|
|
|
---
|
|
|
|
|
2020-06-08 19:51:58 +00:00
|
|
|
[Binary search](https://en.wikipedia.org/wiki/Binary_search_algorithm) can be used on monotonic functions for a logarithmic runtime.
|
2020-06-05 22:24:55 +00:00
|
|
|
|
2020-06-04 05:39:49 +00:00
|
|
|
<!-- END DESCRIPTION -->
|
2020-06-05 22:43:24 +00:00
|
|
|
|
2020-06-09 04:01:24 +00:00
|
|
|
## Basic Application
|
2020-06-05 23:02:56 +00:00
|
|
|
|
2020-06-09 04:01:24 +00:00
|
|
|
Here is a very basic form of binary search:
|
|
|
|
|
|
|
|
> Find an element in a sorted array of size $N$ in $O(\log N)$ time.
|
|
|
|
|
|
|
|
Other variations are similar, such as the following:
|
|
|
|
|
|
|
|
> Given $K$, find the largest element less than $K$ in a sorted array.
|
2020-06-08 18:07:48 +00:00
|
|
|
|
|
|
|
## Tutorial
|
|
|
|
|
2020-06-08 19:51:58 +00:00
|
|
|
- CSES 3.3
|
|
|
|
- [Topcoder Binary Search](https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/)
|
|
|
|
- [CSAcademy Binary Search](https://csacademy.com/lesson/binary_search)
|
|
|
|
- [KA Binary Search](https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/binary-search)
|
|
|
|
- [GeeksForGeeks](https://www.geeksforgeeks.org/binary-search/)
|
2020-06-08 18:07:48 +00:00
|
|
|
|
|
|
|
### Library Functions to do Binary Search
|
|
|
|
|
2020-06-05 23:11:34 +00:00
|
|
|
#### Java
|
2020-06-08 18:07:48 +00:00
|
|
|
|
2020-06-05 23:11:34 +00:00
|
|
|
- [Arrays.binarySearch](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html)
|
|
|
|
- [Collections.binarySearch](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html)
|
2020-06-08 18:07:48 +00:00
|
|
|
|
2020-06-05 23:11:34 +00:00
|
|
|
#### C++
|
2020-06-08 18:07:48 +00:00
|
|
|
|
2020-06-05 23:11:34 +00:00
|
|
|
- [lower_bound](http://www.cplusplus.com/reference/algorithm/lower_bound/)
|
|
|
|
- [upper_bound](http://www.cplusplus.com/reference/algorithm/upper_bound/)
|
2020-06-08 18:07:48 +00:00
|
|
|
|
2020-06-05 23:02:56 +00:00
|
|
|
### Problems
|
2020-06-16 01:44:03 +00:00
|
|
|
|
2020-06-05 23:04:17 +00:00
|
|
|
- [USACO Silver Counting Haybales](http://www.usaco.org/index.php?page=viewproblem2&cpid=666)
|
2020-06-05 23:02:56 +00:00
|
|
|
|
2020-06-08 18:07:48 +00:00
|
|
|
## Binary Searching on the Answer
|
|
|
|
|
|
|
|
Oftentimes used when you need to find the minimum or maximum of some quantity such that it satisfies some property.
|
2020-06-07 07:38:45 +00:00
|
|
|
|
2020-06-08 18:07:48 +00:00
|
|
|
### Tutorial
|
2020-06-08 19:51:58 +00:00
|
|
|
|
2020-06-08 18:07:48 +00:00
|
|
|
- Intro to USACO 12.1
|
|
|
|
|
2020-06-05 23:02:56 +00:00
|
|
|
### Problems
|
2020-06-08 18:07:48 +00:00
|
|
|
|
2020-06-09 04:01:24 +00:00
|
|
|
- USACO
|
|
|
|
- [USACO Silver - Cow Dance](http://www.usaco.org/index.php?page=viewproblem2&cpid=690)
|
|
|
|
- binary search on $K$ and simulate
|
|
|
|
- [USACO Silver - Convention](http://www.usaco.org/index.php?page=viewproblem2&cpid=858)
|
|
|
|
- determine whether $M$ buses suffice if every cow waits at most $T$ minutes
|
|
|
|
- use a greedy strategy (applies to next two problems as well)
|
|
|
|
- [USACO Silver - Angry Cows](http://usaco.org/index.php?page=viewproblem2&cpid=594)
|
|
|
|
- check in $O(N)$ how many haybales you can destroy with fixed radius $R$
|
|
|
|
- [USACO Silver - Social Distancing](http://www.usaco.org/index.php?page=viewproblem2&cpid=1038)
|
|
|
|
- check in $O(N+M)$ how many cows you can place with distance $D$
|
|
|
|
- [USACO Silver - Loan Repayment](http://www.usaco.org/index.php?page=viewproblem2&cpid=991)
|
|
|
|
- requires some rather tricky analysis to speed up naive $O(N\log N)$ solution
|
|
|
|
- CF
|
|
|
|
- [The Meeting Place Cannot Be Changed](http://codeforces.com/contest/782/problem/B) [](48)
|
|
|
|
- [Preparing for Merge Sort](http://codeforces.com/contest/847/problem/B) [](53)
|
|
|
|
- [Level Generation](http://codeforces.com/problemset/problem/818/F) [](54)
|
|
|
|
- [Packmen](http://codeforces.com/contest/847/problem/E) [](57)
|
|
|
|
- [Office Keys](http://codeforces.com/problemset/problem/830/A) [](60)
|