clean up intro

This commit is contained in:
Benjamin Qi 2020-06-18 21:15:13 -04:00
parent fea6ff643b
commit 01a6ed8d06
7 changed files with 172 additions and 167 deletions

View file

@ -5,17 +5,9 @@ author: Darren Yao
description: Learn about the basic data types needed for competitive programming.
---
<module-excerpt>
There are several main **data types** that are used in contests: 32-bit and 64-bit integers, floating point numbers, booleans, characters, and strings. Assuming that you are familiar with the language you are using, this should be mostly review.
There are several main **data types** that are used in contests: 32-bit and 64-bit integers, floating point numbers, booleans, characters, and strings.
</module-excerpt>
## Reading
- CPH 1.1 - 1.3
The **32-bit integer** supports values between $-2\,147\,483\,648$ and $2\,147\,483\,647$, which is roughly equal to $\pm$ $2 \times 10^9$. If the input, output, or \textit{any intermediate values used in calculations} exceed the range of a 32-bit integer, then a **64-bit integer** must be used. The range of the 64-bit integer is between $-9\,223\,372\,036\,854\,775\,808$ and $9\,223\,372\,036\,854\,775\,807$ which is roughly equal to $\pm$ $9 \times 10^{18}$. Contest problems are usually set such that the 64-bit integer is sufficient. If it's not, the problem will ask for the answer modulo $m$, instead of the answer itself, where $m$ is a prime. In this case, make sure to use 64-bit integers, and take the remainder of $x$ modulo $m$ after every step using `x %= m;`.
The **32-bit integer** supports values between $-2\,147\,483\,648$ and $2\,147\,483\,647$, which is roughly equal to $\pm$ $2 \times 10^9.$ If the input, output, or *any intermediate values used in calculations* exceed the range of a 32-bit integer, then a **64-bit integer** must be used. The range of the 64-bit integer is between $-9\,223\,372\,036\,854\,775\,808$ and $9\,223\,372\,036\,854\,775\,807$ which is roughly equal to $\pm$ $9 \times 10^{18}$. Contest problems are usually set such that the 64-bit integer is sufficient. If it's not, the problem will ask for the answer modulo $m$, instead of the answer itself, where $m$ is a prime. In this case, make sure to use 64-bit integers, and take the remainder of $x$ modulo $m$ after every step using `x %= m;`.
**Floating point numbers** are used to store decimal values. It is important to know that floating point numbers are not exact, because the binary architecture of computers can only store decimals to a certain precision. Hence, we should always expect that floating point numbers are slightly off. Contest problems will accommodate this by either asking for the greatest integer less than $10^k$ times the value, or will mark as correct any output that is within a certain $\epsilon$ of the judge's answer.
@ -23,4 +15,8 @@ The **32-bit integer** supports values between $-2\,147\,483\,648$ and $2\,147\,
**Character** variables represent a single Unicode character. They are returned when you access the character at a certain index within a string. Characters are represented using the ASCII standard, which assigns each character to a corresponding integer. This allows us to do arithmetic with them; for example, both `cout << ('f' - 'a');` in C++ and `System.out.print('f' - 'a');` in Java will print `5`.
**Strings** are stored as an array of characters. You can easily access the character at a certain index and take substrings of the string. String problems on USACO Bronze or Silver generally don't involve any special data structures.
**Strings** are stored as an array of characters. You can easily access the character at a certain index and take substrings of the string. String problems on USACO Bronze or Silver generally don't involve any special data structures.
## Additional Reading
- CPH 1.1 - 1.3

View file

@ -12,14 +12,9 @@ prerequisites:
- Intro - Data Types
-
- Intro - Input & Output
description: Solutions for an "USACO Bronze - Fence Painting" in multiple languages.
---
<module-excerpt>
Solutions for an example USACO problem in multiple languages.
</module-excerpt>
[Technical Specifications for USACO Contests](http://www.usaco.org/index.php?page=instructions)
## Example: [Fence Painting](http://usaco.org/index.php?page=viewproblem2&cpid=567)
@ -28,7 +23,7 @@ USACO will automatically add a newline to the end of your file if it does not en
### C++
You can use `ios_base::sync_with_stdio(0); cin.tie(0);` to speed up input and output. See [here](https://codeforces.com/blog/entry/5217) and [StackOverflow](https://stackoverflow.com/questions/31162367/significance-of-ios-basesync-with-stdiofalse-cin-tienull) for more information. Apparently if this is included then it is supposedly prohibited to use `freopen` to redirect `cin` and `cout`, but it works properly on USACO (and I believe that it does in fact result in a significant speedup on large input files).
[Here](https://www.geeksforgeeks.org/bitsstdc-h-c/) is some info about `<bits/stdc++.h\>` if you are not familiar with it.
#### Method 1
@ -54,6 +49,10 @@ int main() {
}
```
Also, including `ios_base::sync_with_stdio(0); cin.tie(0);` in the main function can speed up input & output significantly on some tasks. See [here](https://codeforces.com/blog/entry/5217) and [StackOverflow](https://stackoverflow.com/questions/31162367/significance-of-ios-basesync-with-stdiofalse-cin-tienull) for more information.
- Actually, the former link says that it is supposedly prohibited to use `freopen` to redirect `cin` and `cout` if `ios_base::sync_with_stdio(0); cin.tie(0);` is included, but it works properly as far as I know.
#### Method 2
Use [ifstream & ofstream](http://www.cplusplus.com/doc/tutorial/files/).
@ -77,9 +76,6 @@ int main() {
### Java
Class name can be whatever you want. (?)
```java
import java.io.*;
import java.util.*;
@ -104,7 +100,9 @@ public class paintSol { // must be declared in paintSol.java
}
```
Alternatively, an InputReader class that functions very similarly to Scanner but has the faster runtime of BufferedReader.
Alternatively, we can use the InputReader given in the previous module.
<spoiler title="InputReader">
```java
import java.util.*;
@ -117,7 +115,7 @@ public class template {
StringTokenizer tokenizer;
public InputReader() throws FileNotFoundException {
reader = new BufferedReader(new FileReader("template.in"));
reader = new BufferedReader(new FileReader("paint.in"));
tokenizer = null;
}
@ -148,7 +146,7 @@ public class template {
public static void main(String[] args) throws FileNotFoundException, IOException {
InputReader r = new InputReader();
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("template.out")));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("paint.out")));
int[] cover = new int[100];
for (int i = a; i < b; i++) cover[i] = 1;
@ -162,9 +160,11 @@ public class template {
}
```
</spoiler>
### Python 3
See [here](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files).
See [here](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) for documentation about file I/O.
```py
fin = open("paint.in","r")

View file

@ -5,14 +5,6 @@ author: Nathan Wang, Benjamin Qi, Darren Yao
description: Welcome to the guide! Here we'll explain what a programming competition is, how it works, and how to choose a language.
---
<module-excerpt>
- Introduction
- Contest Format
- Choosing a Language
</module-excerpt>
## Introduction
A **programming competition** generally lasts for several hours and consists of a set of problems. These problems are not open problems; they have already been solved by the problem writer(s) and tester(s) and (hopefully) are designed to be solved in the short timeframe of a contest. In general, each problem in competitive programming is solved by a two-step process:
@ -28,7 +20,7 @@ For those of you with experience in software development, note that competitive
- [William Lin - What is Competitive Programming?](https://www.youtube.com/watch?time_continue=1&v=ueNT-w7Oluw)
- [Kamil Debowski - Interview with a Competitive Programmer](https://www.youtube.com/watch?v=F4rykKLcduI)
- [Example Problem](https://open.kattis.com/contests/mcpc19open/problems/basketballoneonone)
- [Task](https://open.kattis.com/contests/mcpc19open/problems/basketballoneonone) that was mentioned in video.
## Contest Format
@ -36,16 +28,14 @@ The [USA Computing Olympiad](http://www.usaco.org/index.php?page=contests) is a
## Choosing a Language
If you're in Bronze, **don't worry about the language!** If you already know a language, just use it.
The most popular languages that USACO supports are C++11, Java, and Python 3. In general, we recommend the following:
In general, we recommend the following:
- If you already know one of these languages, just use it.
- If you know multiple languages, we recommend you pick C++ over Java, and Java over Python.
- For Bronze, any language will do.
- For Silver, Gold, and Platinum, Python is not recommended.
- For Bronze contestants, any of C++/Java/Python will do.
- If you know multiple languages, we recommend you pick C++ over Java, and Java over Python.
- For Silver, Gold, and Platinum, we recommend C++/Java.
- If you know multiple languages, we recommend you pick C++ over Java.
A majority of high level contestants use C++ and Java. Between those, C++ is more popular. Keep in mind that it's easy to switch languages down the road! Don't get caught up on which language to choose. Just pick the one you feel most comfortable with!
Keep in mind that it's easy to switch languages down the road. Don't get caught up on which language to choose. Just pick the one you feel most comfortable with!
### Language References

View file

@ -2,119 +2,34 @@
id: io
title: Input & Output
author: Darren Yao
description: Demonstrates how to read input and print output for USACO.
---
<module-excerpt>
## Standard I/O
Demonstrates how to read input and print output for USACO.
In most websites (such as CodeForces and CSES), input and output are **standard**.
</module-excerpt>
### C++
## C++
In CodeForces and CSES, input and output are **standard**, meaning that using the library [<iostream\>](http://www.cplusplus.com/reference/iostream/) suffices.
However, in USACO, input is read from a file called `problemname.in`, and printing output to a file called `problemname.out`. Note that you'll have to rename the `.in` and `.out` files. You will need the [<cstdio\>](http://www.cplusplus.com/reference/cstdio/) or the [<fstream\>](http://www.cplusplus.com/reference/fstream/) library. Essentially, replace every instance of the word `problemname` in the word below with the input/output file name, which should be given in the problem. For example, for [this problem](http://www.usaco.org/index.php?page=viewproblem2&cpid=1035), you would replace `problemname` with `socdist1` to get `socdist1.in` and `socdist1.out`.
In order to test a program, create a file called `problemname.in`, and then run the program. The output will be printed to `problemname.out`.
Below, we have included C++ templates for input and output. We use `using namespace std;` so that we don't have to preface standard library functions with `std::` each time we use them.
If `<cstdio>` is used:
The [<iostream\>](http://www.cplusplus.com/reference/iostream/) library suffices.
```cpp
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
freopen("problemname.in", "r", stdin);
freopen("problemname.out", "w", stdout);
// rest of your code ...
int x; cin >> x;
cout << "FOUND " << x << "\n";
}
```
If `<fstream>` is used (Note that you cannot use C-style I/O (scanf, prinf) with this method):
### Java
```cpp
#include <fstream>
In your CS classes, you've probably implemented input and output using standard input and standard output, or using [`Scanner`](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) to read input and `System.out.print` to print output. These methods work, but `Scanner` and `System.out.print` are slow when we have to handle inputting and outputting tens of thousands of lines. Thus, we use `BufferedReader` and `PrintWriter` instead, which are faster because they buffer the input and output and handle it all at once as opposed to parsing each line individually.
using namespace std;
Here is a Java template for input and output, which is effectively a faster Scanner. We import the entire `util` and `io` libraries for ease of use.
int main() {
ifstream cin("problemname.in");
ofstream cout("problemname.out");
// rest of your code ...
}
```
For CodeForces, CSES, and other contests that use standard input and output, simply use the standard input / output from `<iostream>`.
## Java
In your CS classes, you've probably implemented input and output using standard input and standard output, or using `Scanner` to read input and `System.out.print` to print output.
In CodeForces and CSES, input and output are standard, and the above methods work. However, `Scanner` and `System.out.print` are slow when we have to handle inputting and outputting tens of thousands of lines. Thus, we use `BufferedReader` and `PrintWriter` instead, which are faster because they buffer the input and output and handle it all at once as opposed to parsing each line individually.
However, in USACO, input is read from a file called `problemname.in`, and printing output to a file called `problemname.out`. Note that you'll have to rename the `.in` and `.out` files. Essentially, replace every instance of the word *template* in the word below with the input/output file name, which should be given in the problem.
In order to test a program, create a file called `problemname.in`, and then run the program. The output will be printed to `problemname.out`.
Below, we have included Java templates for input and output, which are effectively faster Scanners. We import the entire `util` and `io` libraries for ease of use.
```java
import java.util.*;
import java.io.*;
public class template {
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader() throws FileNotFoundException {
reader = new BufferedReader(new FileReader("template.in"));
tokenizer = null;
}
String next() { // reads in the next String
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() { // reads in the next int
return Integer.parseInt(next());
}
public long nextLong() { // reads in the next long
return Long.parseLong(next());
}
public double nextDouble() { // reads in the next double
return Double.parseDouble(next());
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
InputReader r = new InputReader();
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("template.out")));
// YOUR CODE HERE
pw.close(); // flushes the output once printing is done
}
}
```
For CodeForces, CSES, and other contests that use standard input and output, the template is as follows:
<spoiler title="Standard I/O">
```java
import java.io.*;
@ -166,9 +81,116 @@ public class template {
}
```
</spoiler>
Here's a brief description of the methods in our `InputReader` class, with an instance `r`, and `PrintWriter` with an instance `pw`.
(insert table)
| Method | Description |
| ------------------ | ------------------------------------------------------------------- |
| `r.next()` | Reads the next token (up to a whitespace) and returns a `String` |
| `r.nextInt()` | Reads the next token (up to a whitespace) and returns as an `int` |
| `r.nextLong()` | Reads the next token (up to a whitespace) and returns as a `long` |
| `r.nextDouble()` | Reads the next token (up to a whitespace) and returns as a `double` |
| `pw.println()` | Prints the argument to designated output stream and adds newline |
| `pw.print()` | Prints the argument to designated output stream |
## File I/O
In USACO, input is read from a file called `problemname.in`, and output is printed to a file called `problemname.out`. Note that you'll have to rename the `.in` and `.out` files. For example, for [this problem](http://www.usaco.org/index.php?page=viewproblem2&cpid=1035), you would replace `problemname` with `socdist1` to get `socdist1.in` and `socdist1.out`.
In order to test a program, create a file called `problemname.in`, and then run the program. The output will be printed to `problemname.out`.
## C++
You will need the [<cstdio\>](http://www.cplusplus.com/reference/cstdio/) or the [<fstream\>](http://www.cplusplus.com/reference/fstream/) library. Essentially, replace every instance of the word `problemname` in the word below with the input/output file name, which should be given in the problem.
Below, we have included C++ templates for input and output. We use `using namespace std;` so that we don't have to preface standard library functions with `std::` each time we use them.
If `<cstdio>` is used:
```cpp
#include <cstdio>
using namespace std;
int main() {
freopen("problemname.in", "r", stdin);
freopen("problemname.out", "w", stdout);
// rest of your code ...
}
```
If `<fstream>` is used: (Note that you cannot use C-style I/O (`scanf`, `printf`) with this method):
```cpp
#include <fstream>
using namespace std;
int main() {
ifstream fin("problemname.in");
ofstream fout("problemname.out");
// rest of your code ...
}
```
## Java
We can modify the template above to support file I/O.
<spoiler title="File I/O">
```java
import java.util.*;
import java.io.*;
public class template {
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader() throws FileNotFoundException {
reader = new BufferedReader(new FileReader("template.in"));
tokenizer = null;
}
String next() { // reads in the next String
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() { // reads in the next int
return Integer.parseInt(next());
}
public long nextLong() { // reads in the next long
return Long.parseLong(next());
}
public double nextDouble() { // reads in the next double
return Double.parseDouble(next());
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
InputReader r = new InputReader();
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("template.out")));
// YOUR CODE HERE
pw.close(); // flushes the output once printing is done
}
}
```
</spoiler>
Here's an example to show how input/output works. Let's say we want to write a program that takes three numbers as input and prints their sum.

View file

@ -2,15 +2,10 @@
id: practicing
title: How to Practice
author: Many
description: How to practice, when to read editorials (analyses), etc.
---
<module-excerpt>
How to practice, when to read editorials (analyses), etc.
</module-excerpt>
Knowing when to "give up" on a problem and start reading the problem's editorial is challenging. Below are the opinions of various individuals. Note that "give up" is in quotes, because one still learns when they "give up" and read an editorial!
Knowing when to "give up" on a problem and start reading its editorial is challenging. Below are the opinions of various individuals. Note that "give up" is in quotes, because one still learns when they "give up" and read an editorial!
## Darren Yao (Intro to USACO 1.3)

View file

@ -2,32 +2,29 @@
id: prereqs
title: Prerequisites
author: Nathan Wang, Benjamin Qi
description: Here's what you should know before continuing onwards.
---
<module-excerpt>
Here's what you should learn before reading these resources.
</module-excerpt>
These resources do **not** teach you how to code. We recommend you learn roughly the first half of AP Computer Science A before continuing. If you do not meet these prerequisites, you can go to the resources below to get started.
Familiarity with [competition math](https://github.com/bqi343/USACO/blob/master/Resources/Competition%20Math.md) (ex. AIME qualification) is helpful but not required.
## Expected Knowledge
This guide assumes that you know the basics of how to code, including the following topics:
- Variables
- Data types
- Reading Input
- Writing Output
- Loops
- If/else
- If / Else
- Logical operators
- Functions
- Basic Recursion (a function calling itself)
- Arrays
- Multidimensional Arrays
In particular, contestants using Java should be familiar with roughly the first half of AP Computer Science A. If you do not meet these prerequisites, see the links below to get started.
Familiarity with [competition math](https://github.com/bqi343/USACO/blob/master/Resources/Competition%20Math.md) (ex. AIME qualification) is helpful but not required.
## Resources for Learning How to Code
[Sololearn](https://www.sololearn.com/) has courses on C++, Java, and Python. You don't have to complete the full course.
@ -37,7 +34,7 @@ Familiarity with [competition math](https://github.com/bqi343/USACO/blob/master/
[[info | Pro Tip]]
| You do not need to learn pointers (for now). Knowledge of structs and classes is useful but not required.
### Resources for Getting Started
## Resources for Getting Started
Note: We don't agree with all views expressed in the links below. Feel free to let us know what works (or doesn't) for you.
@ -47,4 +44,4 @@ Note: We don't agree with all views expressed in the links below. Feel free to l
- lots of links!
- [IOI - Getting Started](https://ioinformatics.org/page/getting-started/14)
- not so up to date
- [Quora - Schedule for Beginners (Joshua Pan)](https://www.quora.com/What-is-a-good-schedule-to-follow-for-becoming-better-at-competitive-programming-for-beginners)
- [Quora - Schedule for Beginners (Joshua Pan)](https://www.quora.com/What-is-a-good-schedule-to-follow-for-becoming-better-at-competitive-programming-for-beginners)

View file

@ -30,11 +30,16 @@ Of course, you can't use file I/O on the latter two websites.
## On Mac
[Clang](https://en.wikipedia.org/wiki/Clang) is the default compiler for Mac OS X, but you should use [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection)'s g++.
[Clang](https://en.wikipedia.org/wiki/Clang) is the default compiler for Mac OS X, but you should use [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection)'s g++ since that's what [USACO](http://www.usaco.org/index.php?page=instructions) uses to compile your code.
### Installation
First, Open **Terminal**. Familiarize yourself with some basic commands given [here](https://blog.teamtreehouse.com/introduction-to-the-mac-os-x-command-line). You will also need to install [Homebrew](https://brew.sh/).
First, open **Terminal** and familiarize yourself with some basic commands.
- [Intro to OS X Command Line](https://blog.teamtreehouse.com/introduction-to-the-mac-os-x-command-line).
- [Mac Terminal Cheat Sheet](https://www.makeuseof.com/tag/mac-terminal-commands-cheat-sheet/)
You will also need to install [Homebrew](https://brew.sh/).
Run
@ -89,11 +94,11 @@ However, you can also use [MinGW](http://mingw.org/) if you prefer compiling and
First, download and run the [MinGW installer](https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/). Once it's installed, open the MinGW Installation Manager, click on Basic Setup on the left, and select `mingw32-gcc-g++-bin` for installation.
(Add MinGW to PATH: https://www.rose-hulman.edu/class/csse/resources/MinGW/installation.htm)
[Adding MinGW to PATH](https://www.rose-hulman.edu/class/csse/resources/MinGW/installation.htm)
### Installing WSL
https://code.visualstudio.com/docs/cpp/config-wsl (difficult for beginners)
[VSCode Docs](https://code.visualstudio.com/docs/cpp/config-wsl) (difficult for beginners)
## On Linux
@ -105,9 +110,7 @@ whereis g++
If it is not preinstalled, you can probably install it using your distro's package manager.
## Using the command line
(add tutorial or video?)
## Running With the Command Line
Consider a simple program such as the following, which we'll save in `name.cpp`.
@ -197,6 +200,8 @@ Note that all occurrences of `$1` are replaced with `name`.
## Text Editors
Run with the command line.
- [Sublime Text 3](https://www.sublimetext.com/)
- a fast, lightweight text editor for Windows, Mac, and Linux
- [Editing Build Settings](https://stackoverflow.com/questions/23789410/how-to-edit-sublime-text-build-settings)