minor
This commit is contained in:
parent
d41d12cbb9
commit
d3ad9e208e
|
@ -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
|
|||
<resource source="GFG" url="write-a-c-program-to-print-all-permutations-of-a-given-string" title="Printing all Permutations of Given String"> </resource>
|
||||
</resources>
|
||||
|
||||
## What's Lexicographical Order?
|
||||
## Lexicographical Order
|
||||
|
||||
This term is mentioned quite frequently:
|
||||
|
||||
<problems-list problems={metadata.problems.ex} />
|
||||
|
||||
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
|
|||
|
||||
<CPPSection>
|
||||
|
||||
($O(N\cdot N!)$ code)
|
||||
|
||||
<IncompleteSection />
|
||||
|
||||
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?)
|
||||
|
||||
</CPPSection>
|
||||
|
||||
|
@ -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();
|
||||
|
|
|
@ -40,9 +40,11 @@ Both Java and C++ contain two versions of sets and maps; one in which the keys a
|
|||
|
||||
## HashSets
|
||||
|
||||
### C++
|
||||
<LanguageSection>
|
||||
|
||||
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.
|
||||
<CPPSection>
|
||||
|
||||
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<int> s;
|
||||
|
@ -64,7 +66,9 @@ cout << endl;
|
|||
// You can iterate through an unordered set, but it will do so in arbitrary order
|
||||
```
|
||||
|
||||
### Java
|
||||
</CPPSection>
|
||||
|
||||
<JavaSection>
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
</JavaSection>
|
||||
|
||||
</LanguageSection>
|
||||
|
||||
## HashMaps
|
||||
|
||||
<problems-list problems={metadata.problems.ex} />
|
||||
|
||||
### C++
|
||||
<LanguageSection>
|
||||
|
||||
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.
|
||||
<CPPSection>
|
||||
|
||||
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<int, int> m;
|
||||
|
@ -106,6 +116,11 @@ cout << m.count(7) << '\n' ; // 0
|
|||
cout << m.count(1) << '\n' ; // 1
|
||||
```
|
||||
|
||||
|
||||
</CPPSection>
|
||||
|
||||
<JavaSection>
|
||||
|
||||
### 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,10 +136,18 @@ System.out.println(map.containsKey(7)); // false
|
|||
System.out.println(map.containsKey(1)); // true
|
||||
```
|
||||
|
||||
</JavaSection>
|
||||
|
||||
</LanguageSection>
|
||||
|
||||
## 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.
|
||||
|
||||
<LanguageSection>
|
||||
|
||||
<CPPSection>
|
||||
|
||||
<resources>
|
||||
<resource source="Mark Nelson" title="Hash Functions for C++ Unordered Containers" url="https://marknelson.us/posts/2011/09/03/hash-functions-for-c-unordered-containers.html" starred>How to create user-defined hash function for `unordered_map`.</resource>
|
||||
<resource title="Blowing up Unordered Map" source="CF" url="blog/entry/62393" starred>Explanation of this problem and how to fix it.</resource>
|
||||
|
@ -148,6 +171,17 @@ unordered_map<int,int,chash> U;
|
|||
|
||||
(explain assumptions that are required for this to work)
|
||||
|
||||
|
||||
</CPPSection>
|
||||
|
||||
<JavaSection>
|
||||
|
||||
|
||||
|
||||
</JavaSection>
|
||||
|
||||
</LanguageSection>
|
||||
|
||||
## Problems
|
||||
|
||||
<problems-list problems={metadata.problems.standard} />
|
|
@ -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++
|
||||
<LanguageSection>
|
||||
|
||||
- [lower_bound](http://www.cplusplus.com/reference/algorithm/lower_bound/)
|
||||
- [upper_bound](http://www.cplusplus.com/reference/algorithm/upper_bound/)
|
||||
<CPPSection>
|
||||
|
||||
### Java
|
||||
<resources>
|
||||
<resource source="CPP" url="http://www.cplusplus.com/reference/algorithm/lower_bound/" title="lower_bound"> </resource>
|
||||
<resource source="CPP" url="http://www.cplusplus.com/reference/algorithm/upper_bound/" title="upper_bound"> </resource>
|
||||
</resources>
|
||||
|
||||
- [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)
|
||||
</CPPSection>
|
||||
|
||||
<JavaSection>
|
||||
|
||||
<resources>
|
||||
<resource source="JAVA" url="https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#binarySearch(int[],%20int)" title="Arrays.binarySearch"> </resource>
|
||||
<resource source="JAVA" url="https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#binarySearch(java.util.List,%20T)" title="Collections.binarySearch"> </resource>
|
||||
</resources>
|
||||
|
||||
</JavaSection>
|
||||
|
||||
</LanguageSection>
|
||||
|
||||
## Coordinate Compression
|
||||
|
||||
|
|
|
@ -58,6 +58,8 @@ Usually, when using a greedy algorithm, there is a **heuristic** or **value func
|
|||
|
||||
(what's a heuristic or value function?)
|
||||
|
||||
<IncompleteSection />
|
||||
|
||||
## Example: Studying Algorithms
|
||||
|
||||
### Statement
|
||||
|
|
Reference in a new issue