binsearch

This commit is contained in:
Benjamin Qi 2020-06-24 19:24:24 -04:00
parent eb2e627ead
commit 515adb7ba5
3 changed files with 46 additions and 35 deletions

View file

@ -7,13 +7,32 @@ prerequisites:
description: Traversing a graph in a way such that vertices closer to the starting vertex are processed first.
---
- [CSES Message Route](https://cses.fi/problemset/task/1667)
import { Problem } from "../models";
export const metadata = {
problems: {
sample: [
new Problem("CSES", "Message Route", "1667", "Easy", false, ["BFS"]),
],
general: [
new Problem("CSES", "Monsters", "1194", "Easy", false, ["BFS"]),
new Problem("Gold", "Cow Navigation", "695", "Easy", false, ["BFS"], "lots of casework"),
new Problem("CSA", "BFS-DFS", "bfs-dfs", "Normal", false, ["BFS", "DFS"]),
new Problem("Gold", "Lasers", "671", "Normal", false, ["BFS"]),
new Problem("Gold", "Dream", "575", "Hard", false, ["BFS"]),
],
}
};
<info-block title="Pro Tip">
No silver problem should require BFS rather than DFS, but it's still good to know at this level.
</info-block>
### Tutorial
## Sample
<problems-list problems={metadata.problems.sample} />
## Tutorial
- CPH 12.2
- [PAPS 12.1](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
@ -22,14 +41,6 @@ No silver problem should require BFS rather than DFS, but it's still good to kno
- [cp-algo 0/1 BFS](https://cp-algorithms.com/graph/01_bfs.html)
- [KhanAcademy BFS](https://www.khanacademy.org/computing/computer-science/algorithms/breadth-first-search/a/breadth-first-search-and-its-uses)
### Problems
## Problems
- USACO Gold
- [Monsters](https://cses.fi/problemset/task/1194)
- [Cow Navigation](http://www.usaco.org/index.php?page=viewproblem2&cpid=695)
- [Dream](http://www.usaco.org/index.php?page=viewproblem2&cpid=575)
- lots of casework
- [Lasers](http://www.usaco.org/index.php?page=viewproblem2&cpid=671)
- [Visit FJ](http://www.usaco.org/index.php?page=viewproblem2&cpid=717)
- Other
- [CSAcademy BFS-DFS](https://csacademy.com/contest/round-41/task/bfs-dfs/) [](50)
<problems-list problems={metadata.problems.general} />

View file

@ -7,6 +7,27 @@ prerequisites:
description: You should already be familiar with the concept of binary searching for a number in a sorted array. However, binary search can be extended to binary searching on the answer itself.
---
import { Problem } from "../models";
export const metadata = {
problems: {
general: [
new Problem("CSES", "Factory Machines", "1620", "Easy", false, []),
new Problem("Silver", "Moo Buzz", "966", "Intro", false, [], "binary search not required"),
new Problem("Silver", "Cow Dance Show", "690", "Normal", false, [], "binary search on $K$ and simulate"),
new Problem("Silver", "Convention", "858", "Normal", false, [], "determine whether $M$ buses suffice if every cow waits at most $T$ minutes, use a greedy strategy (applies to next two problems as well)"),
new Problem("Silver", "Angry Cows", "594", "Normal", false, [], "check in $O(N)$ how many haybales you can destroy with fixed radius $R$"),
new Problem("Silver", "Social Distancing", "1038", "Normal", false, [], "check in $O(N+M)$ how many cows you can place with distance $D$"),
new Problem("Silver", "Loan Repayment", "991", "Hard", false, [], "requires some rather tricky analysis to speed up naive $O(N\log N)$ solution"),
new Problem("CF", "The Meeting Place Cannot Be Changed", "contest/782/problem/B", "Normal", false, [], ""),
new Problem("CF", "Preparing for Merge Sort", "contest/847/problem/B", "Normal", false, [], ""),
new Problem("CF", "Level Generation", "problemset/problem/818/F", "Normal", false, [], ""),
new Problem("CF", "Packmen", "contest/847/problem/E", "Normal", false, [], ""),
new Problem("CF", "Office Keys", "problemset/problem/830/A", "Normal", false, [], ""),
],
}
};
When we binary search on the answer, we start with a search space of size $N$ which we know the answer lies in. Then, each iteration of the binary search cuts the search space in half, so the algorithm tests $O(\log N)$ values. This is efficient and much better than testing each possible value in the search space.
Let's say we have a function `check(x)` that returns true if the answer of $x$ is possible, and false otherwise. Usually, in such problems, we'll want to find the maximum or minimum value of $x$ such that `check(x)` is true. Similarly to how binary search on an array only works on a sorted array, binary search on the answer only works if the answer function is [monotonic](https://en.wikipedia.org/wiki/Monotonic_function), meaning that it is always non-decreasing or always non-increasing.
@ -129,26 +150,4 @@ int main() {
### Problems
- USACO Silver
- [Moo Buzz](http://www.usaco.org/index.php?page=viewproblem2&cpid=966)
- [Cow Dance Show](http://www.usaco.org/index.php?page=viewproblem2&cpid=690)
- binary search on $K$ and simulate
- [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)
- [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$
- [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$
- [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)
<problems-list>
<problem name="Factory Machines" cses="1620" difficulty="Intro" />
</problems-list>
<problems-list problems={metadata.problems.general} />

View file

@ -32,6 +32,7 @@ Use *Dijkstra's Algorithm*.
- [CSES Flight Routes](https://cses.fi/problemset/task/1196)
- [CSES Investigation](https://cses.fi/problemset/task/1202)
- USACO
- [Visit FJ](http://www.usaco.org/index.php?page=viewproblem2&cpid=717)
- [Milk Pumping](http://www.usaco.org/index.php?page=viewproblem2&cpid=969)
- fairly standard application
- [Shortcut](http://usaco.org/index.php?page=viewproblem2&cpid=899)