2.9 KiB
2.9 KiB
slug | title | author | order | prerequisites | ||
---|---|---|---|---|---|---|
/silver/binary-search | Binary Search | Nathan Chen | 4 |
|
Binary search can be used on monotonic functions for a logarithmic runtime.
Basic Application
Here is a very basic form of binary search:
Find an element in a sorted array of size
N
inO(\log N)
time.
Other variations are similar, such as the following:
Given
K
, find the largest element less thanK
in a sorted array.
Tutorial
Library Functions to do Binary Search
Java
C++
Problems
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.
Tutorial
- Intro to USACO 12.1
Problems
- USACO
- USACO Silver - Cow Dance
- binary search on
K
and simulate
- binary search on
- USACO Silver - Convention
- determine whether
M
buses suffice if every cow waits at mostT
minutes - use a greedy strategy (applies to next two problems as well)
- determine whether
- USACO Silver - Angry Cows
- check in
O(N)
how many haybales you can destroy with fixed radiusR
- check in
- USACO Silver - Social Distancing
- check in
O(N+M)
how many cows you can place with distanceD
- check in
- USACO Silver - Loan Repayment
- requires some rather tricky analysis to speed up naive
O(N\log N)
solution
- requires some rather tricky analysis to speed up naive
- USACO Silver - Cow Dance
- CF