From d3ad9e208e9ae37a3d49604ea68fd2cd5fc63084 Mon Sep 17 00:00:00 2001 From: Benjamin Qi Date: Tue, 14 Jul 2020 16:01:22 -0400 Subject: [PATCH] minor --- content/3_Bronze/Gen_Perm.mdx | 16 ++++++-- content/3_Bronze/Unordered.mdx | 48 +++++++++++++++++++---- content/4_Silver/Binary_Search_Sorted.mdx | 26 ++++++++---- content/4_Silver/Greedy.mdx | 2 + 4 files changed, 75 insertions(+), 17 deletions(-) diff --git a/content/3_Bronze/Gen_Perm.mdx b/content/3_Bronze/Gen_Perm.mdx index d71fdc5..4e418ba 100644 --- a/content/3_Bronze/Gen_Perm.mdx +++ b/content/3_Bronze/Gen_Perm.mdx @@ -13,6 +13,9 @@ export const metadata = { permSam: [ new Problem("CSES", "Creating Strings I", "1622", "Intro|Easy", false, [], "all perms of string"), ], + ex: [ + new Problem("Bronze", "Photoshoot", "988", "Normal", false, [], ""), + ], perm: [ new Problem("CSES", "Chessboard and Queens", "1624", "Normal", false, [], "Recursively backtrack. See CSES book for more details."), new Problem("Bronze", "Livestock Lineup", "965", "Hard", false, ["permutations"], ""), // 91.95 @@ -30,7 +33,11 @@ A **permutation** is a reordering of a list of elements. Some problems will ask -## What's Lexicographical Order? +## Lexicographical Order + +This term is mentioned quite frequently: + + Think about how are words ordered in a dictionary. (In fact, this is where the term "lexicographical" comes from.) @@ -56,6 +63,10 @@ What's going to be in the `check` function depends on the problem, but it should +($O(N\cdot N!)$ code) + + + We can just use the `next_permutation()` function. This function takes in a range and modifies it to the next greater permutation. If there is no greater permutation, it returns false. To iterate through all permutations, place it inside a `do-while` loop. We are using a `do-while` loop here instead of a typical `while` loop because a `while` loop would modify the smallest permutation before we got a chance to process it. ```cpp @@ -64,7 +75,7 @@ do { } while(next_permutation(v.begin(), v.end())); ``` -This makes $O(N!)$ swaps. +This makes $O(N!)$ swaps. (why?) @@ -88,7 +99,6 @@ public class Test { used[i] = false; cur.remove(cur.size()-1); } } - static void genPerm(int _n) { n = _n; used = new boolean[n]; gen(); diff --git a/content/3_Bronze/Unordered.mdx b/content/3_Bronze/Unordered.mdx index 93e2885..e86f517 100644 --- a/content/3_Bronze/Unordered.mdx +++ b/content/3_Bronze/Unordered.mdx @@ -40,9 +40,11 @@ Both Java and C++ contain two versions of sets and maps; one in which the keys a ## HashSets -### C++ + -The operations on an unordered set are `insert`, which adds an element to the set if not already present, `erase`, which deletes an element if it exists, and `count`, which returns `1` if the set contains the element and `0` if it doesn't. + + +The operations on an [`unordered_set`](http://www.cplusplus.com/reference/unordered_set/unordered_set/) are `insert`, which adds an element to the set if not already present, `erase`, which deletes an element if it exists, and `count`, which returns `1` if the set contains the element and `0` if it doesn't. ```cpp unordered_set s; @@ -64,7 +66,9 @@ cout << endl; // You can iterate through an unordered set, but it will do so in arbitrary order ``` -### Java + + + The operations on an unordered set are `add`, which adds an element to the set if not already present, `remove`, which deletes an element if it exists, and `contains`, which checks whether the set contains that element. @@ -87,13 +91,19 @@ for(int element : set){ // You can iterate through an unordered set, but it will do so in arbitrary order ``` + + + + ## HashMaps -### C++ + -In an unordered map `m`, the `m[key] = value` operator assigns a value to a key and places the key and value pair into the map. The operator `m[key]` returns the value associated with the key. If the key is not present in the map, then `m[key]` is set to 0. The `count(key)` method returns the number of times the key is in the map (which is either one or zero), and therefore checks whether a key exists in the map. Lastly, `erase(key)` and `erase(it)` removes the map entry associated with the specified key or iterator. All of these operations are $O(1)$, but again, due to the hashing, this has a high constant factor. + + +In an [`unordered_map`](http://www.cplusplus.com/reference/unordered_map/unordered_map/) `m`, the `m[key] = value` operator assigns a value to a key and places the key and value pair into the map. The operator `m[key]` returns the value associated with the key. If the key is not present in the map, then `m[key]` is set to 0. The `count(key)` method returns the number of times the key is in the map (which is either one or zero), and therefore checks whether a key exists in the map. Lastly, `erase(key)` and `erase(it)` removes the map entry associated with the specified key or iterator. All of these operations are $O(1)$, but again, due to the hashing, this has a high constant factor. ```cpp unordered_map m; @@ -106,6 +116,11 @@ cout << m.count(7) << '\n' ; // 0 cout << m.count(1) << '\n' ; // 1 ``` + + + + + ### Java In the unordered map, the `put(key, value)` method assigns a value to a key and places the key and value pair into the map. The `get(key)` method returns the value associated with the key. The `containsKey(key)` method checks whether a key exists in the map. Lastly, `remove(key)` removes the map entry associated with the specified key. All of these operations are $O(1)$, but again, due to the hashing, this has a high constant factor. @@ -121,13 +136,21 @@ System.out.println(map.containsKey(7)); // false System.out.println(map.containsKey(1)); // true ``` + + + + ## Hacking In USACO contests, unordered sets and maps generally fine, but the built-in hashing algorithm for C++ is vulnerable to pathological data sets causing abnormally slow runtimes. Apparently [Java](https://codeforces.com/blog/entry/62393?#comment-464875) is not vulnerable to this, however. + + + + - How to create user-defined hash function for `unordered_map`. - Explanation of this problem and how to fix it. + How to create user-defined hash function for `unordered_map`. + Explanation of this problem and how to fix it. ### Implementation @@ -148,6 +171,17 @@ unordered_map U; (explain assumptions that are required for this to work) + + + + + + + + + + + ## Problems \ No newline at end of file diff --git a/content/4_Silver/Binary_Search_Sorted.mdx b/content/4_Silver/Binary_Search_Sorted.mdx index f6df84b..4ae9f56 100644 --- a/content/4_Silver/Binary_Search_Sorted.mdx +++ b/content/4_Silver/Binary_Search_Sorted.mdx @@ -2,7 +2,7 @@ id: binary-search-sorted title: "Binary Search on a Sorted Array" author: Siyong Huang, Michael Cao, Nathan Chen -description: Introduces sorting and binary searching on a sorted array. +description: "?" prerequisites: - Bronze - Introduction to Data Structures frequency: 4 @@ -34,15 +34,27 @@ Suppose that we want to find an element in a sorted array of size $N$ in $O(\log ## Library Functions -### C++ + - - [lower_bound](http://www.cplusplus.com/reference/algorithm/lower_bound/) - - [upper_bound](http://www.cplusplus.com/reference/algorithm/upper_bound/) + -### 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) + + + + + + + + + + + + ## Coordinate Compression diff --git a/content/4_Silver/Greedy.mdx b/content/4_Silver/Greedy.mdx index 0cdbb61..774fe4c 100644 --- a/content/4_Silver/Greedy.mdx +++ b/content/4_Silver/Greedy.mdx @@ -58,6 +58,8 @@ Usually, when using a greedy algorithm, there is a **heuristic** or **value func (what's a heuristic or value function?) + + ## Example: Studying Algorithms ### Statement