Bronze / Silver

This commit is contained in:
Benjamin Qi 2020-07-14 22:11:50 -04:00
parent 249733bfbc
commit dba84f76cf
11 changed files with 110 additions and 93 deletions

View file

@ -80,6 +80,7 @@ Vim is probably the easiest way to print syntax-highlighted code on Mac, see the
### Sublime Text Notes (Ben)
- I prefer the **Mariana** color scheme in place of the default.
- open command palette (Cmd-Shift-P) -> change color scheme
- [`subl` symlink](https://www.sublimetext.com/docs/3/osx_command_line.html)
- Using `/usr/local/bin/subl` instead of `~/bin/subl` worked for me on OS X Mojave.
- [Snippets](https://www.granneman.com/webdev/editors/sublime-text/top-features-of-sublime-text/quickly-insert-text-and-code-with-sublime-text-snippets)

View file

@ -22,8 +22,8 @@ export const metadata = {
<resources>
<resource source="IUSACO" title="4.1 - Dynamic Arrays">module is based off this</resource>
<resource source="CPH" title="4.1 - Dynamic Arrays" starred></resource>
<resource source="PAPS" title="3.1 - Vectors, 6.1 - Dynamic Arrays">ow dynamic arrays work</resource>
<resource source="CPH" title="4.1 - Dynamic Arrays" starred>vectors, strings</resource>
<resource source="PAPS" title="3.1 - Vectors, 6.1 - Dynamic Arrays">how dynamic arrays work</resource>
<resource source="CPC" title="2 - Data Structures" url="02_data_structures">assumes prior experience</resource>
</resources>

View file

@ -8,20 +8,20 @@ frequency: 2
import { Problem } from "../models";
export const metadata = {
problems: {
blocked: [
new Problem("Bronze", "Blocked Billboard", "759", "Easy", false, ["rect"]),
],
general: [
problems: {
blocked: [
new Problem("Bronze", "Blocked Billboard", "759", "Easy", false, ["rect"]),
],
general: [
new Problem("Bronze", "Square Pasture", "663", "Very Easy", false, ["rect"]),
new Problem("Bronze", "Blocked Billboard II", "783", "Easy", false, ["rect"]),
new Problem("CF", "Div. 3 C - White Sheet", "contest/1216/problem/C", "Normal", false, ["rect"],"See this code (TODO; codeforces is down) for a nice implementation using the Java Rectangle class."),
]
}
]
}
};
<resources>
<resource source="IUSACO" title="7.1 - Rectangle Geometry">module is based off this</resource>
<resource source="IUSACO" title="7.1 - Rectangle Geometry">module is based off this</resource>
</resources>
<br/>
@ -78,7 +78,7 @@ Of course, this wouldn't suffice if the coordinates were up to $10^9$.
### Rectangle Class
A useful class in `Java` for dealing with rectangle geometry problems (though definitely overkill) is the built-in [`Rectangle`](https://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html) class.
A useful class in Java for dealing with rectangle geometry problems (though definitely overkill) is the built-in [`Rectangle`](https://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html) class.
<LanguageSection>
@ -93,11 +93,11 @@ Rectangle newRect = new Rectangle(x, y, width, height);
The `Rectangle` class supports numerous useful methods.
- `firstRect.intersects(secondRect)` checks if two rectangles intersect.
- `firstRect.union(secondRect)` returns a rectangle representing the union of two rectangles.
- `firstRect.contains(x, y)` checks whether the integer point $(x,y)$ exists in firstRect.
- `firstRect.intersection(secondRect)` returns a rectangle representing the intersection of two rectangles.
- what happens when intersection is empty?
- `firstRect.intersects(secondRect)` checks if two rectangles intersect.
- `firstRect.union(secondRect)` returns a rectangle representing the union of two rectangles.
- `firstRect.contains(x, y)` checks whether the integer point $(x,y)$ exists in firstRect.
- `firstRect.intersection(secondRect)` returns a rectangle representing the intersection of two rectangles.
- what happens when intersection is empty?
This class can often lessen the implementation needed in a lot of bronze problems and CodeForces problems.
@ -111,29 +111,29 @@ import java.io.*;
import java.util.*;
public class blockedBillboard {
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(new File("billboard.in"));
PrintWriter pw = new PrintWriter(new FileWriter("billboard.out"));
int x1, y1, x2, y2;
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(new File("billboard.in"));
PrintWriter pw = new PrintWriter(new FileWriter("billboard.out"));
int x1, y1, x2, y2;
// the top left point is (0,0), so you need to do -y2
x1 = sc.nextInt(); y1 = sc.nextInt(); x2 = sc.nextInt(); y2 = sc.nextInt();
Rectangle firstRect = new Rectangle(x1, -y2, x2-x1, y2-y1);
x1 = sc.nextInt(); y1 = sc.nextInt(); x2 = sc.nextInt(); y2 = sc.nextInt();
Rectangle secondRect = new Rectangle(x1, -y2, x2-x1, y2-y1);
x1 = sc.nextInt(); y1 = sc.nextInt(); x2 = sc.nextInt(); y2 = sc.nextInt();
Rectangle truck = new Rectangle(x1, -y2, x2-x1, y2-y1);
// the top left point is (0,0), so you need to do -y2
x1 = sc.nextInt(); y1 = sc.nextInt(); x2 = sc.nextInt(); y2 = sc.nextInt();
Rectangle firstRect = new Rectangle(x1, -y2, x2-x1, y2-y1);
x1 = sc.nextInt(); y1 = sc.nextInt(); x2 = sc.nextInt(); y2 = sc.nextInt();
Rectangle secondRect = new Rectangle(x1, -y2, x2-x1, y2-y1);
x1 = sc.nextInt(); y1 = sc.nextInt(); x2 = sc.nextInt(); y2 = sc.nextInt();
Rectangle truck = new Rectangle(x1, -y2, x2-x1, y2-y1);
long firstIntersect = getArea(firstRect.intersection(truck));
long secondIntersect = getArea(secondRect.intersection(truck));
pw.println(getArea(firstRect)+getArea(secondRect)
- firstIntersect - secondIntersect);
pw.close();
}
public static long getArea(Rectangle r){
if (r.isEmpty()) return 0;
return (long)r.getHeight()*(long)r.getWidth();
}
long firstIntersect = getArea(firstRect.intersection(truck));
long secondIntersect = getArea(secondRect.intersection(truck));
pw.println(getArea(firstRect)+getArea(secondRect)
- firstIntersect - secondIntersect);
pw.close();
}
public static long getArea(Rectangle r){
if (r.isEmpty()) return 0;
return (long)r.getHeight()*(long)r.getWidth();
}
}
```
</spoiler>
@ -145,28 +145,28 @@ import java.io.*;
import java.util.*;
class Rect {
int x1, y1, x2, y2;
Rect() {}
int x1, y1, x2, y2;
Rect() {}
int area() { return (y2-y1)*(x2-x1); }
}
public class blockedBillboard {
public static void main (String[] args) throws IOException{
Scanner sc = new Scanner(new File("billboard.in"));
PrintWriter pw = new PrintWriter(new FileWriter("billboard.out"));
Rect a = new Rect(), b = new Rect(), t = new Rect();
int x1, y1, x2, y2;
a.x1 = sc.nextInt(); a.y1 = sc.nextInt(); a.x2 = sc.nextInt(); a.y2 = sc.nextInt();
b.x1 = sc.nextInt(); b.y1 = sc.nextInt(); b.x2 = sc.nextInt(); b.y2 = sc.nextInt();
t.x1 = sc.nextInt(); t.y1 = sc.nextInt(); t.x2 = sc.nextInt(); t.y2 = sc.nextInt();
pw.println(area(a) + area(b) - intersect(a, t) - intersect(b, t));
pw.close();
}
static int area(Rect r){ return (r.y2 - r.y1) * (r.x2 - r.x1); }
static int intersect(Rect p, Rect q) {
int xOverlap = Math.max(0, Math.min(p.x2, q.x2) - Math.max(p.x1, q.x1));
int yOverlap = Math.max(0, Math.min(p.y2, q.y2) - Math.max(p.y1, q.y1));
return xOverlap * yOverlap;
}
public static void main (String[] args) throws IOException{
Scanner sc = new Scanner(new File("billboard.in"));
PrintWriter pw = new PrintWriter(new FileWriter("billboard.out"));
Rect a = new Rect(), b = new Rect(), t = new Rect();
int x1, y1, x2, y2;
a.x1 = sc.nextInt(); a.y1 = sc.nextInt(); a.x2 = sc.nextInt(); a.y2 = sc.nextInt();
b.x1 = sc.nextInt(); b.y1 = sc.nextInt(); b.x2 = sc.nextInt(); b.y2 = sc.nextInt();
t.x1 = sc.nextInt(); t.y1 = sc.nextInt(); t.x2 = sc.nextInt(); t.y2 = sc.nextInt();
pw.println(a.area()+b.area()-intersect(a, t)-intersect(b, t));
pw.close();
}
static int intersect(Rect p, Rect q) {
int xOverlap = Math.max(0, Math.min(p.x2, q.x2) - Math.max(p.x1, q.x1));
int yOverlap = Math.max(0, Math.min(p.y2, q.y2) - Math.max(p.y1, q.y1));
return xOverlap * yOverlap;
}
}
```
</spoiler>
@ -175,41 +175,33 @@ public class blockedBillboard {
<CPPSection>
Unfortunately, C++ doesn't have a built in rectangle class, so you need to write the functions yourself. Here is the solution to Blocked Billboard written in C++ (thanks, Brian Dean!).
Unfortunately, C++ doesn't have a built in rectangle class, so you need to write the functions yourself. Here is the solution to Blocked Billboard written in C++ (thanks, Brian Dean!). A C++ [`struct`](http://www.cplusplus.com/doc/tutorial/structures/) is the same as a C++ `class` but all members are `public` rather than `private` by default.
<spoiler title="C++ Solution">
```cpp
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
struct Rect{
int x1, y1, x2, y2;
struct Rect {
int x1,y1,x2,y2;
int area() { return (y2-y1)*(x2-x1); }
};
int area(Rect r){
return (r.y2 - r.y1) * (r.x2 - r.x1);
}
int intersect(Rect p, Rect q){
int xOverlap = max(0, min(p.x2, q.x2) - max(p.x1, q.x1));
int yOverlap = max(0, min(p.y2, q.y2) - max(p.y1, q.y1));
return xOverlap * yOverlap;
int xOverlap = max(0,min(p.x2,q.x2)-max(p.x1,q.x1));
int yOverlap = max(0,min(p.y2,q.y2)-max(p.y1,q.y1));
return xOverlap*yOverlap;
}
int main(){
ifstream cin ("billboard.in");
ofstream cout ("billboard.out");
Rect a, b, t; // billboards a, b, and the truck
cin >> a.x1 >> a.y1 >> a.x2 >> a.y2;
cin >> b.x1 >> b.y1 >> b.x2 >> b.y2;
cin >> t.x1 >> t.y1 >> t.x2 >> t.y2;
cout << area(a) + area(b) - intersect(a, t) - intersect(b, t) << endl;
return 0;
Rect a, b, t; // billboards a, b, and the truck
cin >> a.x1 >> a.y1 >> a.x2 >> a.y2;
cin >> b.x1 >> b.y1 >> b.x2 >> b.y2;
cin >> t.x1 >> t.y1 >> t.x2 >> t.y2;
cout << a.area()+b.area()-intersect(a,t)-intersect(b,t) << endl;
}
```
</spoiler>

View file

@ -35,7 +35,7 @@ export const metadata = {
<br />
Since there's no formal algorithm involved, the intent of the problem is to assess competence with one's programming language of choice and knowledge of built-in data structures. At least in USACO Bronze, when a problem statement says to find the end result of some process, or to find when something occurs, it's usually sufficient to **simulate** the process naively.
Since there's no formal algorithm involved, the intent of the problem is to assess competence with one's programming language of choice and knowledge of built-in data structures. At least in USACO Bronze, when a problem statement says to find the end result of some process, or to find when something occurs, it's usually sufficient to simulate the process naively.
## Example 1

View file

@ -4,7 +4,7 @@ title: "Binary Search on the Answer"
author: Darren Yao, Abutalib Namazov
prerequisites:
- Silver - Binary Search on a Sorted Array
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.
description: "?"
frequency: 3
---

View file

@ -5,7 +5,7 @@ author: Siyong Huang, Michael Cao, Nathan Chen
description: "?"
prerequisites:
- Bronze - Introduction to Data Structures
frequency: 4
frequency: 2
---
import { Problem } from "../models";

View file

@ -19,7 +19,9 @@ What if we want to use a C++ `set` with the `Edge` struct that was defined in "S
## Operator Overloading
Works as expected, although you should make sure to include the second `const` or you'll get a compilation error. (explain meaning of this)
Works as expected, although you should make sure to include the second `const` or you'll get a compilation error. From the link above:
> [The second const] means you cannot modify member variables of the current object.
```cpp
#include <bits/stdc++.h>
@ -127,7 +129,7 @@ int main() {
## Built-In Functors
Overloading `<` automatically generates the functor `less<Edge>`. Similarly, overloading `>` automatically generates the functor [`greater<Edge>`](https://en.cppreference.com/w/cpp/utility/functional/greater). We can use this to store a set in reverse order.
Overloading the less than operator (`<`) automatically generates the functor `less<Edge>`. Similarly, overloading `>` automatically generates the functor [`greater<Edge>`](https://en.cppreference.com/w/cpp/utility/functional/greater). We can use this to store a set in reverse order.
```cpp
#include <bits/stdc++.h>

View file

@ -125,6 +125,8 @@ int main() {
</spoiler>
<IncompleteSection />
## Problems
<problems-list problems={metadata.problems.general} />

View file

@ -4,7 +4,7 @@ title: "Introduction to Ordered Sets"
author: Darren Yao, Benjamin Qi
prerequisites:
- Bronze - Unordered Maps & Sets
description: ""
description: "?"
frequency: 2
---
@ -52,8 +52,6 @@ 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).
</CPPSection>
<JavaSection>
@ -75,12 +73,12 @@ set.remove(set.higher(6)); // [1, 2, 14]
System.out.println(set.higher(23); // ERROR, no such element exists
```
One 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 - Point Update Range Sum).
</JavaSection>
</LanguageSection>
One 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 - Point Update Range Sum).
## Ordered Maps
<LanguageSection>
@ -189,9 +187,27 @@ static void remove(int x){
</LanguageSection>
## Using Iterators
<LanguageSection>
<CPPSection>
next(), prev(), ++, --
</CPPSection>
<JavaSection>
</JavaSection>
</LanguageSection>
<IncompleteSection />
## Standard
<problems-list problems={metadata.problems.standard} />

View file

@ -43,7 +43,7 @@ export const metadata = {
new Problem("CSES", "Range XOR Queries", "1650", "Easy", false, ["Prefix Sums"], "XOR prefix[r] with prefix[l-1]"),
],
complex: [
new Problem("Google Kick Start", "Candies (Test Set 1)", "https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff43/0000000000337b4d", "Easy", false, ["Prefix Sums"], ""),
new Problem("Google KickStart", "Candies (Test Set 1)", "https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff43/0000000000337b4d", "Easy", false, ["Prefix Sums"], ""),
new Problem("AC", "Multiple of 2019", "https://atcoder.jp/contests/abc164/tasks/abc164_d", "Hard", false, ["Prefix Sums"], "Make use of the fact that adding 0's to the end of a number does not affect whether it is a multiple of 2019 (because 10 and 2019 are coprime)."),
],
}
@ -395,7 +395,13 @@ $$
+ \texttt{prefix}[a-1][b-1]
$$
Summing the blue region from above using the 2d prefix sums method, we add the value of the green square, subtract the values of the red squares, and then add the value of the gray square. In this example, we have $65-23-6+1 = 37$, as expected.
Summing the blue region from above using the 2D prefix sums method, we add the value of the green square, subtract the values of the red squares, and then add the value of the gray square. In this example, we have
$$
65-23-6+1 = 37,
$$
as expected.
<center>
@ -446,7 +452,7 @@ Summing the blue region from above using the 2d prefix sums method, we add the v
</center>
Since no matter the size of the submatrix we are summing, we only need to access four values of the 2d prefix sum array, this runs in $O(1)$ per query after an $O(NM)$ preprocessing. This is fast enough.
Since no matter the size of the submatrix we are summing, we only need to access four values of the 2D prefix sum array, this runs in $O(1)$ per query after an $O(NM)$ preprocessing.
<problems-list problems={metadata.problems.cum2} />

View file

@ -105,8 +105,6 @@ First, we need to define a **class** that represents what we want to sort. In ou
<CPPSection>
A C++ [`struct`](http://www.cplusplus.com/doc/tutorial/structures/) is the same as a C++ `class` but all members are `public` rather than `private` by default.
```cpp
#include <bits/stdc++.h>
using namespace std;