From cfe38d9312c81deda70d774fb5eb2e629d8be132 Mon Sep 17 00:00:00 2001 From: Benjamin Qi Date: Mon, 29 Jun 2020 17:03:24 -0400 Subject: [PATCH] bronze --- content/1_Intro/Ex_Prob.mdx | 8 +++++- content/3_Bronze/Intro_Graphs.mdx | 2 +- content/3_Bronze/Intro_Sorting.mdx | 41 ++++++++++++++++++------------ content/3_Bronze/Pairs_Tuples.mdx | 1 - content/4_Silver/Stacks_Queues.mdx | 5 ++-- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/content/1_Intro/Ex_Prob.mdx b/content/1_Intro/Ex_Prob.mdx index 96c6243..41b98e8 100644 --- a/content/1_Intro/Ex_Prob.mdx +++ b/content/1_Intro/Ex_Prob.mdx @@ -32,10 +32,16 @@ export const metadata = { -Importantly, USACO will automatically add a newline to the end of your file if it does not end with one. **Make sure not to output trailing spaces!** +Importantly, USACO will automatically add a newline to the end of your file if it does not end with one. + + + +Make sure not to output trailing spaces, or you will get an error such as the following: ![bad](./Error.png) + + ### C++ [Here](https://www.geeksforgeeks.org/bitsstdc-h-c/) is some info about `` if you are not familiar with it. diff --git a/content/3_Bronze/Intro_Graphs.mdx b/content/3_Bronze/Intro_Graphs.mdx index 4c30ee6..25e1976 100644 --- a/content/3_Bronze/Intro_Graphs.mdx +++ b/content/3_Bronze/Intro_Graphs.mdx @@ -11,7 +11,7 @@ import { Problem } from "../models"; export const metadata = { problems: { general: [ - new Problem("Silver", "Grass Planting", "894", "Easy", false, ["tree"]), + new Problem("Silver", "Grass Planting", "894", "Normal", false, ["tree"]), new Problem("Bronze", "The Great Revegetation", "916", "Hard", false, []), new Problem("Bronze", "Milk Factory", "940", "Hard", false, ["tree"]), new Problem("Bronze", "Swapity Swap", "1013", "Hard", false, ["permutation"], "Hint: One option is to keep swapping until the permutation returns to its original state (but can you do better?)."), diff --git a/content/3_Bronze/Intro_Sorting.mdx b/content/3_Bronze/Intro_Sorting.mdx index 10c00fe..ecb2bc7 100644 --- a/content/3_Bronze/Intro_Sorting.mdx +++ b/content/3_Bronze/Intro_Sorting.mdx @@ -14,6 +14,9 @@ export const metadata = { new Problem("HR", "Bubble Sort", "https://www.hackerrank.com/challenges/ctci-bubble-sort/problem", "Easy", false, [], "O(N^2)"), new Problem("Silver", "Out of Sorts", "834", "Very Hard", false, []), ], + count: [ + new Problem("Silver", "Counting Haybales", "666", "Normal", false, []), + ], cses: [ new Problem("CSES", "Apartments", "1084", "Normal", false, [], "Sort applicants and apartments, then greedily assign applicants"), new Problem("CSES", "Ferris Wheel", "1090", "Normal", false, [], "Sort children, keep a left pointer and a right pointer. Each gondola either is one child from the right pointer or two children, one left and one right."), @@ -50,17 +53,18 @@ There are many sorting algorithms, here are some sources to learn about the popu - [HackerEarth Mergesort](https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/tutorial/) - $O(N\log N)$ -## Library Sorting +## Library Functions - Sorting - C++ - - [std::sort Documentation](https://en.cppreference.com/w/cpp/algorithm/sort) - - [std::stable\_sort documentation](http://www.cplusplus.com/reference/algorithm/stable_sort/) + - [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort) + - [std::stable\_sort](http://www.cplusplus.com/reference/algorithm/stable_sort/) - [Golovanov399 - C++ Tricks](https://codeforces.com/blog/entry/74684) - first two related to sorting - Java - - [Arrays.sort Documentation](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[])) + - [Arrays.sort](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[])) + - [Collections.sort](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List)) - Python - - [Sorted Documentation](https://docs.python.org/3/howto/sorting.html) + - [Sorting Basics](https://docs.python.org/3/howto/sorting.html) @@ -74,7 +78,6 @@ Two ways to avoid this: - ## Binary Search [Binary search](https://en.wikipedia.org/wiki/Binary_search_algorithm) can be used on monotonic (what's that?) functions for a logarithmic runtime. @@ -97,27 +100,33 @@ Other variations are similar, such as the following: -### Library Functions to do Binary Search - -#### Java - - - [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) +### Library Functions - Binary Search #### C++ - [lower_bound](http://www.cplusplus.com/reference/algorithm/lower_bound/) - [upper_bound](http://www.cplusplus.com/reference/algorithm/upper_bound/) -## Example (Coordinate Compression) +#### Java -Another useful application of sorting is coordinate compression, which takes some points and reassigns them to remove wasted space. Let's consider the USACO Silver problem [Counting Haybales](http://www.usaco.org/index.php?page=viewproblem2&cpid=666): + - [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) + +## Coordinate Compression + +Another useful application of sorting is **coordinate compression**, which takes some points and reassigns them to remove wasted space. + + > Farmer John has just arranged his $N$ haybales $(1\le N \le 100,000)$ at various points along the one-dimensional road running across his farm. To make sure they are spaced out appropriately, please help him answer $Q$ queries ($1 \le Q \le 100,000$), each asking for the number of haybales within a specific interval along the road. -However, each of the points are in the range $0 \ldots 1,000,000,000$, meaning you can't store locations of haybales in, for instance, a boolean array. However, let's place all of the locations of the haybales into a list and sort it. +However, each of the points are in the range $0 \ldots 1,000,000,000$, meaning you can't store locations of haybales in, for instance, a boolean array. -(fix this part) +### Solution + +Let's place all of the locations of the haybales into a list and sort it. + +(fix part below so transform to range $1\ldots N$) Now, we can map distinct points to smaller integers without gaps. For example, if the haybales existed at positions $[1, 4, 5, 9]$ and queries were $(1, 2)$ and $(4, 6)$, we can place the integers together and map them from $[1, 2, 4, 5, 6, 9] \rightarrow [1, 2, 3, 4, 5, 6]$. This effectively transforms the haybale positions into $[1, 3, 4, 6]$ and the queries into $1, 2$ and $3, 5$. diff --git a/content/3_Bronze/Pairs_Tuples.mdx b/content/3_Bronze/Pairs_Tuples.mdx index ced80f3..1a5c972 100644 --- a/content/3_Bronze/Pairs_Tuples.mdx +++ b/content/3_Bronze/Pairs_Tuples.mdx @@ -3,7 +3,6 @@ id: pairs-tuples title: Pairs & Tuples author: Aaron Chew, Benjamin Qi, Nathan Wang, Darren Yao description: Introduces pairs, which allow you to store two objects (possibly of different types) as a single unit, as well as tuples, which are a generalization of pairs. -frequency: 0 --- (never needed but uh it's pretty useful lol) diff --git a/content/4_Silver/Stacks_Queues.mdx b/content/4_Silver/Stacks_Queues.mdx index 3cc6537..947ce34 100644 --- a/content/4_Silver/Stacks_Queues.mdx +++ b/content/4_Silver/Stacks_Queues.mdx @@ -22,8 +22,9 @@ export const metadata = { ## Additional Reading - - + + + ## [Stacks](http://www.cplusplus.com/reference/stack/stack/)