rearrange bronze

This commit is contained in:
Benjamin Qi 2020-06-09 00:01:24 -04:00
parent 7969af20bf
commit cb466ca2d6
16 changed files with 489 additions and 106 deletions

View file

@ -168,8 +168,10 @@ Although both Python and Java receive two times the C++ time limit in USACO, thi
</details>
- A comparable C++ solution runs in less than 700ms.
<details>
<summary>Comparable C++ Solution (< 700ms)</summary>
<summary>C++ Solution</summary>
```cpp
#include <bits/stdc++.h>
@ -230,5 +232,5 @@ Although both Python and Java receive two times the C++ time limit in USACO, thi
## Other
- Python lacks a data structure that keeps its keys in sorted order (the equivalent of `set` in C++), which is required for some silver problems.
- Java lacks features such as `#define`, `typedef`, and `auto` that are present in C++ (which some contestants rely on extensively).
- Problemsetters don't always test Java solutions (and rarely Python) when setting the constraints.
- Java lacks features such as `#define`, `typedef`, and `auto` that are present in C++ (which some contestants rely on extensively, see "macros").
- USACO problemsetters don't always test Java solutions (and rarely Python) when setting constraints.

View file

@ -65,7 +65,7 @@ const ll INF = 1e18;
const ld PI = acos((ld)-1);
```
$10^9+7$ is a prime that appears quite frequently in programming contests. Interestingly, ```(MOD-1)/2``` is also prime. On the other hand, $998244353-1$ is divisible by $2^{23}$, which is useful for [NTT](https://en.wikipedia.org/wiki/Discrete_Fourier_transform_(general)#Number-theoretic_transform).
$10^9+7$ is a prime that appears quite frequently in programming contests. Interestingly, $(10^9+7-1)/2=5\cdot 10^8+3$ is also prime. On the other hand, $998244353-1$ is divisible by $2^{23}$, which is useful for [NTT](https://en.wikipedia.org/wiki/Discrete_Fourier_transform_(general)#Number-theoretic_transform).
Most USACO problems satisfy $N\le 2\cdot 10^5$.
@ -75,7 +75,7 @@ Most USACO problems satisfy $N\le 2\cdot 10^5$.
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
```
See [neal's blog](https://codeforces.com/blog/entry/61587) about why `rand()` is bad (use `rng()` instead).
See [neal's blog](https://codeforces.com/blog/entry/61587) about why `rand()` is bad. Uuse `rng()` instead.
### ckmin

View file

@ -2,7 +2,7 @@
slug: /bronze/rectangle-geometry
title: "Rectangle Geometry"
author: Unknown
order: 3
order: 2
---
See 7.1 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a

View file

@ -2,14 +2,14 @@
slug: /bronze/collections
title: Built-In Java Collections
author: Darren Yao
order: 2
order: 3
---
-> Mostly copy and pasted from chapter 4 of my book.
Introduces the data structures in the Java standard library that are frequently used in competitive programming.
<!-- END DESCRIPTION -->
A data structure determines how data is stored. (is it sorted? indexed? what operations does it support?) Each data structure supports some operations efficiently, while other operations are either inefficient or not supported at all. This chapter introduces the data structures in the Java standard library that are frequently used in competitive programming.
A **data structure** determines how data is stored (is it sorted? indexed? what operations does it support?). Each data structure supports some operations efficiently, while other operations are either inefficient or not supported at all.
Java default `Collections` data structures are designed to store any type of object. However, we usually don't want this; instead, we want our data structures to only store one type of data, like integers, or strings. We do this by putting the desired data type within the `<>` brackets when declaring the data structure, as follows:
@ -81,7 +81,7 @@ In array-based contest problems, we'll use one-, two-, and three-dimensional sta
### Stacks
A stack is a Last In First Out (LIFO) data structure that supports three operations: `push`, which adds an element to the top of the stack, `pop`, which removes an element from the top of the stack, and `peek`, which retrieves the element at the top without removing it, all in $O(1)$ time. Think of it like a real-world stack of papers.
A stack is a **Last In First Out** (LIFO) data structure that supports three operations: `push`, which adds an element to the top of the stack, `pop`, which removes an element from the top of the stack, and `peek`, which retrieves the element at the top without removing it, all in $O(1)$ time. Think of it like a real-world stack of papers (or cards).
```java
Stack<Integer> s = new Stack<Integer>();
@ -95,7 +95,7 @@ System.out.println(s.size()); // 2
### Queues
A queue is a First In First Out (FIFO) data structure that supports three operations of `add`, insertion at the back of the queue, `poll`, deletion from the front of the queue, and `peek`, which retrieves the element at the front without removing it, all in $O(1)$ time. Java doesn't actually have a `Queue` class; it's only an interface. The most commonly used implementation is the `LinkedList`, declared as follows: `Queue q = new LinkedList(); ` .
A queue is a First In First Out (FIFO) data structure that supports three operations of `add`, insertion at the back of the queue, `poll`, deletion from the front of the queue, and `peek`, which retrieves the element at the front without removing it, all in $O(1)$ time. Java doesn't actually have a `Queue` class; it's only an interface. The most commonly used implementation is the `LinkedList`, declared as follows:
```java
Queue<Integer> q = new LinkedList<Integer>();
@ -122,7 +122,7 @@ deque.removeLast(); // [1, 3]
### Priority Queues
A priority queue supports the following operations: insertion of elements, deletion of the element considered highest priority, and retrieval of the highest priority element, all in $O(\log n)$ time according to the number of elements in the priority queue. Priority is based on a comparator function, but by default the lowest element is at the front of the priority queue. The priority queue is one of the most important data structures in competitive programming, so make sure you understand how and when to use it. By default, the Priority Queue puts the lowest element at the front of the queue.
A priority queue supports the following operations: insertion of elements, deletion of the element considered lowest priority, and retrieval of the lowest priority element, all in $O(\log n)$ time according to the number of elements in the priority queue. Priority is based on a comparator function, but by default the lowest element is at the front of the priority queue. The priority queue is one of the most important data structures in competitive programming, so make sure you understand how and when to use it.
```java
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
@ -191,7 +191,7 @@ A map is a set of ordered pairs, each containing a key and a value. In a map, al
### Unordered Maps
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.
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.
```java
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

View file

@ -0,0 +1,355 @@
---
slug: /bronze/containers
title: Built-In C++ Containers
author: Darren Yao
order: 4
---
Introduces the data structures in the C++ standard library that are frequently used in competitive programming.
<!-- END DESCRIPTION -->
A **data structure** determines how data is stored (is it sorted? indexed? what operations does it support?). Each data structure supports some operations efficiently, while other operations are either inefficient or not supported at all.
# Containers
The C++ standard library data structures are designed to store any type of data. We put the desired data type within the `<>` brackets when declaring the data structure, as follows:
```cpp
vector<string> v;
```
This creates a `vector` structure that only stores objects of type `string`.
For our examples below, we will primarily use the `int` data type, but note that you can use any data type including `string` and user-defined structures.
Essentially every standard library data structure supports the `size()` method, which returns the number of elements in the data structure, and the `empty()` method, which returns `true` if the data structure is empty, and `false` otherwise.
## Dynamic Arrays
You're probably already familiar with regular (static) arrays. Now, there are also dynamic arrays (`vector` in C++) that support all the functions that a normal array does, and can resize itself to accommodate more elements. In a dynamic array, we can also add and delete elements at the end in $O(1)$ time.
For example, the following code creates a dynamic array and adds the numbers $1$ through $10$ to it:
```cpp
vector<int> v;
for(int i = 1; i <= 10; i++){
v.push_back(i);
}
```
When declaring a dynamic array we can give it an initial size, so it doesn't resize itself as we add elements to it. The following code initializes a `vector` with initial size $30$:
```cpp
vector<int> v(30);
```
However, we need to be careful that we only add elements to the end of the `vector`; insertion and deletion in the middle of the `vector` is $O(n)$.
```cpp
vector<int> v;
v.push_back(2); // [2]
v.push_back(3); // [2, 3]
v.push_back(7); // [2, 3, 7]
v.push_back(5); // [2, 3, 7, 5]
v[1] = 4; // sets element at index 1 to 4 -> [2, 4, 7, 5]
v.erase(v.begin() + 1); // removes element at index 1 -> [2, 7, 5]
// this remove method is O(n); to be avoided
v.push_back(8); // [2, 7, 5, 8]
v.erase(v.end()-1); // [2, 7, 5]
// here, we remove the element from the end of the list; this is O(1).
v.push_back(4); // [2, 7, 5, 4]
v.push_back(4); // [2, 7, 5, 4, 4]
v.push_back(9); // [2, 7, 5, 4, 4, 9]
cout << v[2]; // 5
v.erase(v.begin(), v.begin()+3); // [4, 4, 9]
// this erases the first three elements; O(n)
```
To iterate through a static or dynamic array, we can use either the regular for loop or the for-each loop.
```cpp
vector<int> v;
v.push_back(1); v.push_back(7); v.push_back(4); v.push_back(5); v.push_back(2);
int arr[] = {1, 7, 4, 5, 2};
for(int i = 0; i < v.size(); i++){
cout << v[i] << " ";
}
cout << endl;
for(int element : arr){
cout << element << " ";
}
cout << endl;
```
In order to sort a dynamic array, use `sort(v.begin(), v.end())` (or `sort(begin(v),end(v))`), whereas static arrays require `sort(arr, arr + N)` where $N$ is the number of elements to be sorted. The default sort function sorts the array in ascending order.
In array-based contest problems, we'll use one-, two-, and three-dimensional static arrays most of the time. However, we can also have static arrays of dynamic arrays, dynamic arrays of static arrays, and so on. Usually, the choice between a static array and a dynamic array is just personal preference.
## Iterators
An **iterator** allows you to traverse a container by pointing to an object within the container (although they are **not** the same thing as pointers). For example, `vector.begin()` returns an iterator pointing to the first element of the vector. Apart from the standard way of traversing a vector (by treating it as an array), you can also use iterators:
```cpp
for (vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) {
cout << *it; //prints the values in the vector using the pointer
}
```
C++11 and later versions can automatically infer the type of an object if you use the keyword `auto`. This means that you can replace `vector<int>::iterator` with `auto` or `int` with `auto` in the for-each loop.
```cpp
for(auto element : v) {
cout << element; //prints the values in the vector
}
```
## Stacks and the Various Types of Queues
### [Stacks](http://www.cplusplus.com/reference/stack/stack/)
A stack is a **Last In First Out** (LIFO) data structure that supports three operations, all in $O(1)$ time:
- `push`: adds an element to the top of the stack
- `pop`: removes an element from the top of the stack
- `top`: retrieves the element at the top without removing it
Think of it like a real-world stack of papers (or cards).
```cpp
stack<int> s;
s.push(1); // [1]
s.push(13); // [1, 13]
s.push(7); // [1, 13, 7]
cout << s.top() << endl; // 7
s.pop(); // [1, 13]
cout << s.size() << endl; // 2
```
**Basic Problems**:
- UVa 00514 - Rails
- UVa 00732 - Anagram by Stack
- UVa 01062 - Containers
### [Queues](http://www.cplusplus.com/reference/queue/queue/ )
A queue is a First In First Out (FIFO) data structure that supports three operations, all in $O(1)$ time.
- `push`: insertion at the back of the queue
- `pop`, deletion from the front of the queue
- `front`: which retrieves the element at the front without removing it.
```cpp
queue<int> q;
q.push(1); // [1]
q.push(3); // [3, 1]
q.push(4); // [4, 3, 1]
q.pop(); // [4, 3]
cout << q.front() << endl; // 3
```
### [Deques](http://www.cplusplus.com/reference/deque/deque/)
A deque (usually pronounced "deck") stands for double ended queue and is a combination of a stack and a queue, in that it supports $O(1)$ insertions and deletions from both the front and the back of the deque. The four methods for adding and removing are `push_back`, `pop_back`, `push_front`, and `pop_front`. Not very common in Bronze / Silver.
```cpp
deque<int> d;
d.push_front(3); // [3]
d.push_front(4); // [4, 3]
d.push_back(7); // [4, 3, 7]
d.pop_front(); // [3, 7]
d.push_front(1); // [1, 3, 7]
d.pop_back(); // [1, 3]
```
**Queue/Deque Basic Problems**:
- UVa 10172 - The Lonesome Cargo
- UVa 10901 - Ferry Loading III
- UVa 11034 - Ferry Loading IV
### [Priority Queues](http://www.cplusplus.com/reference/queue/priority_queue/)
A priority queue supports the following operations: insertion of elements, deletion of the element considered highest priority, and retrieval of the highest priority element, all in $O(\log n)$ time according to the number of elements in the priority queue. Priority is based on a comparator function, but by default the highest element is at the top of the priority queue. The priority queue is one of the most important data structures in competitive programming, so make sure you understand how and when to use it.
```cpp
priority_queue<int> pq;
pq.push(7); // [7]
pq.push(2); // [2, 7]
pq.push(1); // [1, 2, 7]
pq.push(5); // [1, 2, 5, 7]
cout << pq.top() << endl; // 7
pq.pop(); // [1, 2, 5]
pq.pop(); // [1, 2]
pq.push(6); // [1, 2, 6]
```
## Sets and Maps
A set is a collection of objects that contains no duplicates. There are two types of sets: unordered sets (`unordered_set` in C++), and ordered set (`set` in C++).
### Unordered Sets
The unordered set works by hashing, which is assigning a unique code to every variable/object which allows insertions, deletions, and searches in $O(1)$ time, albeit with a high constant factor, as hashing requires a large constant number of operations. However, as the name implies, elements are not ordered in any meaningful way, so traversals of an unordered set will return elements in some arbitrary order. 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.
```cpp
unordered_set<int> s;
s.insert(1); // [1]
s.insert(4); // [1, 4] in arbitrary order
s.insert(2); // [1, 4, 2] in arbitrary order
s.insert(1); // [1, 4, 2] in arbitrary order
// the add method did nothing because 1 was already in the set
cout << s.count(1) << endl; // 1
set.erase(1); // [2, 4] in arbitrary order
cout << s.count(5) << endl; // 0
s.erase(0); // [2, 4] in arbitrary order
// if the element to be removed does not exist, nothing happens
for(int element : s){
cout << element << " ";
}
cout << endl;
// You can iterate through an unordered set, but it will do so in arbitrary order
```
### [Ordered Sets](http://www.cplusplus.com/reference/set/set/)
The second type of set data structure is the ordered or sorted set. Insertions, deletions, and searches on the ordered set require $O(\log n)$ time, based on the number of elements in the set. As well as those supported by the unordered set, the ordered set also allows four additional operations: `begin()`, which returns an iterator to the lowest element in the set, `end()`, which returns an iterator to the highest element in the set, `lower_bound`, which returns an iterator to the least element greater than or equal to some element `k`, and `upper_bound`, which returns an iterator to the least element strictly greater than some element `k`.
```cpp
set<int> s;
s.insert(1); // [1]
s.insert(14); // [1, 14]
s.insert(9); // [1, 9, 14]
s.insert(2); // [1, 2, 9, 14]
cout << *s.upper_bound(7) << '\n'; // 9
cout << *s.upper_bound(9) << '\n'; // 14
cout << *s.lower_bound(5) << '\n'; // 9
cout << *s.lower_bound(9) << '\n'; // 9
cout << *s.begin() << '\n'; // 1
auto it = s.end();
cout << *(--it) << '\n'; // 14
s.erase(s.upper_bound(6)); // [1, 2, 14]
```
The primary limitation of the ordered set is that we can't efficiently access the $k^{th}$ largest element in the set, or find the number of elements in the set greater than some arbitrary $x$. These operations can be handled using a data structure called an order statistic tree (see Gold - Binary Indexed Trees).
### [Maps](http://www.cplusplus.com/reference/map/map/)
A map is a set of ordered pairs, each containing a key and a value. In a map, all keys are required to be unique, but values can be repeated. Maps have three primary methods: one to add a specified key-value pairing, one to retrieve the value for a given key, and one to remove a key-value pairing from the map. Like sets, maps can be unordered (`unordered_map` in C++) or ordered (`map` in C++). In an unordered map, hashing is used to support $O(1)$ operations. In an ordered map, the entries are sorted in order of key. Operations are $O(\log n)$, but accessing or removing the next key higher or lower than some input `k` is also supported.
### Unordered Maps
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.
```cpp
unordered_map<int, int> m;
m[1] = 5; // [(1, 5)]
m[3] = 14; // [(1, 5); (3, 14)]
m[2] = 7; // [(1, 5); (3, 14); (2, 7)]
m.erase(2); // [(1, 5); (3, 14)]
cout << m[1] << '\n'; // 5
cout << m.count(7) << '\n' ; // 0
cout << m.count(1) << '\n' ; // 1
```
### Ordered Maps
The ordered map supports all of the operations that an unordered map supports, and additionally supports `lower_bound` and `upper_bound`, returning the iterator pointing to the lowest entry not less than the specified key, and the iterator pointing to the lowest entry strictly greater than the specified key respectively.
```cpp
map<int, int> m;
m[3] = 5; // [(3, 5)]
m[11] = 4; // [(3, 5); (11, 4)]
m[10] = 491; // [(3, 5); (10, 491); (11, 4)]
cout << m.lower_bound(10)->first << " " << m.lower_bound(10)->second << '\n'; // 10 491
cout << m.upper_bound(10)->first << " " << m.upper_bound(10)->second << '\n'; // 11 4
m.erase(11); // [(3, 5); (10, 491)]
if (m.upper_bound(10) == m.end())
{
cout << "end" << endl; // Prints end
}
```
A note on unordered sets and maps: In USACO contests, they're generally fine, but in CodeForces contests, you should always use sorted sets and maps. This is because the built-in hashing algorithm is vulnerable to pathological data sets causing abnormally slow runtimes, in turn causing failures on some test cases (see [neal's blog](https://codeforces.com/blog/entry/62393)). Alternatively, use a different hashing algorithm.
**Basic Problems**:
- UVa 10226 - Hardwood Species
- UVa 11286 - Conformity
- UVa 11572 - Unique Snowflakes
### [Multisets](http://www.cplusplus.com/reference/set/multiset/)
Lastly, there is the multiset, which is essentially a sorted set that allows multiple copies of the same element. In addition to all of the regular set operations, the multiset `count()` method returns the number of times an element is present in the multiset. (Actually, you shouldn't use `count()` because this takes time **linear** in the number of matches.)
The `begin()`, `end()`, `lower_bound()`, and `upper_bound()` operations work the same way they do in the normal sorted set.
**Warning:** If you want to remove a value *once*, make sure to use `multiset.erase(multiset.find(val))` rather than `multiset.erase(val)`. The latter will remove *all* instances of `val`.
```cpp
multiset<int> ms;
ms.insert(1); // [1]
ms.insert(14); // [1, 14]
ms.insert(9); // [1, 9, 14]
ms.insert(2); // [1, 2, 9, 14]
ms.insert(9); // [1, 2, 9, 9, 14]
ms.insert(9); // [1, 2, 9, 9, 9, 14]
cout << ms.count(4) << '\n'; // 0
cout << ms.count(9) << '\n'; // 3
cout << ms.count(14) << '\n'; // 1
ms.erase(ms.find(9));
cout << ms.count(9) << '\n'; // 2
ms.erase(9);
cout << ms.count(9) << '\n'; // 0
```
**Set/Multiset Problems**:
- UVa 00978 - Lemmings Battle
- UVa 11136 - Hoax or what
- UVa 11849 - CD
## [Pair](http://www.cplusplus.com/reference/utility/pair/pair/)
A structure that holds two values, not necessarily of the same type (not built into java!).
- `make_pair(a, b)`: Returns a pair with values a, b.
- `pair.first`: The first value of the pair.
- `pair.second`: The second value of the pair.
Example
```cpp
#include <iostream>
using namespace std;
int main() {
pair<string, int> myPair = make_pair("Testing", 123);
cout << myPair.first << " " << myPair.second << endl; // Testing 123
vector<pair<int,int>> v = {{2,4},{1,3},{3,4},{3,1}};
sort(begin(v),end(v)); // {(1, 3), (2, 4), (3, 1), (3, 4)}
}
/* Output
* Testing 123
*/
```
## Other Resources
- CPH 4
- [CPC.2](https://github.com/SuprDewd/T-414-AFLV/tree/master/02_data_structures)
## Additional Problems
* [Beautiful Triplets](https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/mancunian-and-beautiful-triplets-30968257/) [](54)
* [Polycarp's Phone Book](http://codeforces.com/contest/860/problem/B) [](56)
* [Jury Marks](http://codeforces.com/contest/831/problem/C) [](67)
* [Mahmoud & Ehab & Function](http://codeforces.com/contest/862/problem/E) [](74)
* [Karen & Cards](http://codeforces.com/contest/815/problem/D) [](86)
* [Tournament](http://codeforces.com/contest/878/problem/C) [](106)

View file

@ -0,0 +1,51 @@
---
slug: /bronze/ds
title: Data Structures
author: Nathan Wang, ?
order: 5
---
Problems and additional resources regarding built-in data structures.
<!-- END DESCRIPTION -->
## Problems
(actually go through these ...)
**Stack:**:
- UVa 00514 - Rails
- UVa 00732 - Anagram by Stack
- UVa 01062 - Containers
**Queue/Deque:**:
- UVa 10172 - The Lonesome Cargo
- UVa 10901 - Ferry Loading III
- UVa 11034 - Ferry Loading IV
**Ordered Maps:**:
- UVa 10226 - Hardwood Species
- UVa 11286 - Conformity
- UVa 11572 - Unique Snowflakes
**Set/Multiset:**
- UVa 00978 - Lemmings Battle
- UVa 11136 - Hoax or what
- UVa 11849 - CD
**General:**
- [Beautiful Triplets](https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/mancunian-and-beautiful-triplets-30968257/) [](54)
- [Polycarp's Phone Book](http://codeforces.com/contest/860/problem/B) [](56)
- [Jury Marks](http://codeforces.com/contest/831/problem/C) [](67)
- [Mahmoud & Ehab & Function](http://codeforces.com/contest/862/problem/E) [](74)
- [Karen & Cards](http://codeforces.com/contest/815/problem/D) [](86)
- [Tournament](http://codeforces.com/contest/878/problem/C) [](106)
## Other Resources
- CPH 4
- [CPC.2](https://github.com/SuprDewd/T-414-AFLV/tree/master/02_data_structures)

View file

@ -6,3 +6,5 @@ order: 4
---
See 5 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a
<!-- END DESCRIPTION -->

View file

@ -6,3 +6,5 @@ order: 5
---
See 6 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a
<!-- END DESCRIPTION -->

View file

@ -1,54 +0,0 @@
---
slug: /silver/containers
title: "Containers"
author: Unknown
order: 2
prerequisites:
-
- Bronze - Containers
---
Containers from C++ standard template library (STL)
- CPH 4
- Linear
- Queues / Deques
- C++ Lists?
- Linked List
- Stacks
- Log
- Priority Queues
- BST / TreeSet / TreeMap (Python doesnt have log n map)
- Sets / Maps
- Multiset (F FOR JAVA) (just teach a workaround or smthn ig) TreeMap<value,numoccrances> (erase by value pitfall)
- Custom comparator (prerequisite for basically all silver topics in Java)
- HashSets / HashMaps
- ex?
<!-- END DESCRIPTION -->
(from my github)
* Types
* Set, Multiset, Unordered Set
* Map, Unordered Map (Hashmap)
* see STL demo
* Queue, Deque, Linked List
* Priority Queue (Heap)
* Vector
* Stack
* Tutorial
* CPH (4, Data Structures)
* [C++ Reference](http://www.cplusplus.com/reference/stl/)
* [CPC.2](https://github.com/SuprDewd/T-414-AFLV/tree/master/02_data_structures)
* Problems (sorted by length)
* [Beautiful Triplets](https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/mancunian-and-beautiful-triplets-30968257/) [](54)
* [Polycarp's Phone Book](http://codeforces.com/contest/860/problem/B) [](56)
* [Jury Marks](http://codeforces.com/contest/831/problem/C) [](67)
* [Mahmoud & Ehab & Function](http://codeforces.com/contest/862/problem/E) [](74)
* [Karen & Cards](http://codeforces.com/contest/815/problem/D) [](86)
* [Tournament](http://codeforces.com/contest/878/problem/C) [](106)
[Blowing up Unordered Map](https://codeforces.com/blog/entry/62393)
Java tutorial by Aaron: https://docs.google.com/document/d/1HaqUhVB2Ou3ZK4WRjHd738nof4yvLGfrjZFkCxIQP1A/edit?usp=sharing

View file

@ -14,7 +14,7 @@ prerequisites:
<!-- END DESCRIPTION -->
- Comparators
- Custom Comparators (prerequisite for basically all silver topics in Java)
- CPH 3
- std::sort / Collections.sort
- coord compress

View file

@ -12,9 +12,15 @@ prerequisites:
<!-- END DESCRIPTION -->
## The Basic Application
## Basic Application
"Find an element in a sorted array in $O(\log N)$ time." This is a very basic form of binary search. Other variations are similar, like "Given $K$, find the largest element less than $K$ in a sorted array."
Here is a very basic form of binary search:
> Find an element in a sorted array of size $N$ in $O(\log N)$ time.
Other variations are similar, such as the following:
> Given $K$, find the largest element less than $K$ in a sorted array.
## Tutorial
@ -49,19 +55,21 @@ Oftentimes used when you need to find the minimum or maximum of some quantity su
### Problems
USACO
- [USACO Silver Cownvention](http://www.usaco.org/index.php?page=viewproblem2&cpid=858)
- [USACO Silver Cow Dance](http://www.usaco.org/index.php?page=viewproblem2&cpid=690)
- [USACO Silver Social Distancing](http://www.usaco.org/index.php?page=viewproblem2&cpid=1038)
- [USACO Silver Angry Cows](http://usaco.org/index.php?page=viewproblem2&cpid=594)
- [USACO Silver Loan Repayment](http://www.usaco.org/index.php?page=viewproblem2&cpid=991)
- Also needs some math and rather tricky "sqrt" analysis
Misc
- [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)
- USACO
- [USACO Silver - Cow Dance](http://www.usaco.org/index.php?page=viewproblem2&cpid=690)
- binary search on $K$ and simulate
- [USACO Silver - 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)
- [USACO Silver - 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$
- [USACO Silver - 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$
- [USACO Silver - 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)

View file

@ -21,7 +21,14 @@ prerequisites:
- CSES
- [Sum of Two Values](https://cses.fi/problemset/task/1640)
- [Maximum Subarray Sum](https://cses.fi/problemset/task/1643)
- [Maximum Subarray Sum](https://cses.fi/problemset/task/1643) (<- let's not lump this in w/ two pointers?)
- USACO
- [Silver - Diamond Collector](http://usaco.org/index.php?page=viewproblem2&cpid=643)
- sort and then use 2P
- [Silver - Paired Up](http://usaco.org/index.php?page=viewproblem2&cpid=738)
- sort and then use 2P
- [Gold - Haybale Feast](http://usaco.org/index.php?page=viewproblem2&cpid=767)
- just 2P with additional set
- CF
- [Books](https://codeforces.com/problemset/problem/279/B)
- [Cellular Network](http://codeforces.com/problemset/problem/702/C) [](48)
@ -29,8 +36,6 @@ prerequisites:
- [K-Good Segment](http://codeforces.com/problemset/problem/616/D) [](53)
- [(Long Title)](http://codeforces.com/problemset/problem/814/C) [](54)
- [Jury Meeting](http://codeforces.com/problemset/problem/853/B) [](90)
- USACO
- [Gold Haybale Feast](http://usaco.org/index.php?page=viewproblem2&cpid=767)
## Extensions

View file

@ -75,9 +75,9 @@ order: 7
### Problems
- [Ice Perimeter (Easy)](http://usaco.org/index.php?page=viewproblem2&cpid=895)
- [Switching on the Lights (Moderate)](http://www.usaco.org/index.php?page=viewproblem2&cpid=570)
- [Build Gates (Moderate)](http://www.usaco.org/index.php?page=viewproblem2&cpid=596)
- [Why Did the Cow Cross the Road III, Silver (Moderate)](http://usaco.org/index.php?page=viewproblem2&cpid=716)
- [Switching on the Lights (Normal)](http://www.usaco.org/index.php?page=viewproblem2&cpid=570)
- [Build Gates (Normal)](http://www.usaco.org/index.php?page=viewproblem2&cpid=596)
- [Why Did the Cow Cross the Road III, Silver (Normal)](http://usaco.org/index.php?page=viewproblem2&cpid=716)
- [Multiplayer Moo (Hard)](http://usaco.org/index.php?page=viewproblem2&cpid=836)
## Graph Two-Coloring
@ -216,8 +216,8 @@ int main()
- Try to solve the problem in O(N)!
- [The Bovine Shuffle (Normal)](http://usaco.org/index.php?page=viewproblem2&cpid=764)
- [Swapity Swapity Swap (Very Hard)](http://www.usaco.org/index.php?page=viewproblem2&cpid=1014)
- [CSES Round Trip (undirected)](https://cses.fi/problemset/task/1669)
- [CSES Round Trip II (directed)](https://cses.fi/problemset/task/1678)
- [CSES Round Trip (undirected cycle)](https://cses.fi/problemset/task/1669)
- [CSES Round Trip II (directed cycle)](https://cses.fi/problemset/task/1678)
- POI
- [Mafia](https://szkopul.edu.pl/problemset/problem/w3YAoAT3ej27YeiaNWjK57_G/site/?key=statement)
- [Spies](https://szkopul.edu.pl/problemset/problem/r6tMTfvQFPAEfQioYMCQndQe/site/?key=statement)

View file

@ -10,7 +10,7 @@ order: 0
> the closest OJ for IOI style?
> Do you have any magic problems sets to suggest?
Once you've reached the platinum level, it may be helpful to practice with problems from other (inter)national olympiads.
Once you've reached Platinum, it may be helpful to practice with problems from other (inter)national olympiads.
## National

View file

@ -14,21 +14,33 @@ General range queries for associative operations, including segment tree.
## Static Range Queries
### Range Minimum Query
Given a static array $A[1],A[2],\ldots,A[N]$, you want to answer queries in the form $A[l]\ominus A[l+1]\ominus \cdots \ominus A[r]$ in $O(1)$ time each with $O(N\log N)$ time preprocessing, where $\ominus$ denotes any associative operation. In the case when $\ominus$ denotes `min`, preprocessing can be done in $O(1)$ time.
- Tutorial
- [Wikipedia](https://en.wikipedia.org/wiki/Range_minimum_query)
- (add)
### Tutorial
### General Static Range Queries (Online)
- Static range queries in $O(1)$ time and $O(N\log N)$ preprocessing for any associative operation
- (add)
- [Range Minimum Query](https://en.wikipedia.org/wiki/Range_minimum_query)
- Use for $O(1)$ LCA
- CPH 9.1
- [cp-algorithms RMQ](https://cp-algorithms.com/sequences/rmq.html)
### Divide & Conquer
**Divide & conquer** can refer to many different techniques. In this case, we use it to answer $Q$ queries offline in $O((N+Q)\log N)$ time.
Suppose that all queries satisfiy $L\le l\le r\le R$ (initially, $L=1$ and $R=N$). Letting $M=\left\lfloor \frac{L+R}{2}\right\rfloor$, we can compute
$$lef[l]=A[l]\ominus A[l+1]\ominus \cdots \ominus A[M]$$
for all $L\le l\le M$ and
$$rig[r]=A[M+1]\ominus A[M+2] \ominus \cdots\ominus A[r]$$
for each $M< r\le R$. Then the answer for all queries satisfying $l\le M<r$ is simply $lef[l]\ominus rig[r]$ due to the associativity condition. After that, we recurse on all query intervals completely contained within $[L,M]$ and $[M+1,R]$ independently.
Actually, this can be adjusted to answer queries online in $O(1)$ time each. See my implementation [here](https://github.com/bqi343/USACO/blob/master/Implementations/content/data-structures/Static%20Range%20Queries%20(9.1)/RangeQuery.h).
See my implementation [here](https://github.com/bqi343/USACO/blob/master/Implementations/content/data-structures/Static%20Range%20Queries%20(9.1)/RangeQuery.h).
#### Problems
- [DMOJ Continued Fractions](https://dmoj.ca/problem/dmopc19c7p4)
- [USACO Plat NonDec](http://www.usaco.org/index.php?page=viewproblem2&cpid=997)
- [USACO Plat - NonDec](http://www.usaco.org/index.php?page=viewproblem2&cpid=997)
## Segment Tree