This commit is contained in:
Benjamin Qi 2020-07-13 20:48:09 -04:00
parent c0513f9e25
commit 7aa354b30a
4 changed files with 86 additions and 48 deletions

View file

@ -13,13 +13,13 @@ import { Problem } from "../models";
export const metadata = {
problems: {
fence: [
new Problem("Bronze", "Fence Painting", "567", "Intro"),
new Problem("Bronze", "Fence Painting", "567", "Very Easy"),
],
general: [
new Problem("Bronze", "Promotion Counting", "591", "Intro"),
new Problem("Bronze", "Word Processor", "987", "Intro"),
new Problem("Bronze", "Square Pasture", "663", "Intro"),
new Problem("Bronze", "Bucket Brigade", "939", "Intro"),
new Problem("Bronze", "Promotion Counting", "591", "Very Easy"),
new Problem("Bronze", "Word Processor", "987", "Very Easy"),
new Problem("Bronze", "Square Pasture", "663", "Very Easy"),
new Problem("Bronze", "Bucket Brigade", "939", "Very Easy"),
]
}
};
@ -28,25 +28,24 @@ export const metadata = {
<resource source="USACO" title="Technical Specifications for Contests" url="http://www.usaco.org/index.php?page=instructions" starred>Make sure to read this.</resource>
</resources>
<br />
Importantly, USACO will automatically add a newline to the end of your file if it does not end with one. Make sure not to output trailing spaces, or you will get an error such as the following:
![bad](./Error.png)
## Example: Fence Painting
<problems-list problems={metadata.problems.fence} />
Importantly, USACO will automatically add a newline to the end of your file if it does not end with one.
<info-block title="Pro Tip">
<LanguageSection>
Make sure not to output trailing spaces, or you will get an error such as the following:
![bad](./Error.png)
</info-block>
### C++
<CPPSection>
[Here](https://www.geeksforgeeks.org/bitsstdc-h-c/) is some info about `<bits/stdc++.h>` if you are not familiar with it.
#### Method 1
### Method 1
Use [freopen](http://www.cplusplus.com/reference/cstdio/freopen/). If you comment out both of the lines containing `freopen` then the program reads from standard in and writes to standard out as usual.
@ -71,7 +70,7 @@ int main() {
}
```
#### Method 2
### Method 2
Use [ifstream & ofstream](http://www.cplusplus.com/doc/tutorial/files/).
@ -92,11 +91,14 @@ int main() {
}
```
### Java
</CPPSection>
<JavaSection>
```java
import java.io.*;
import java.util.*;
public class paintSol { // must be declared in paintSol.java
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("paint.in"));
@ -120,8 +122,6 @@ public class paintSol { // must be declared in paintSol.java
Alternatively, we can use the InputReader given in the previous module.
<spoiler title="Solution with InputReader">
```java
import java.util.*;
import java.io.*;
@ -178,9 +178,9 @@ public class template {
}
```
</spoiler>
</JavaSection>
### Python 3
<PySection>
See [here](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) for documentation about file I/O.
@ -200,6 +200,10 @@ for i in range(100):
fout.write(str(ans))
```
</PySection>
</LanguageSection>
## Introductory Problems
The following require relatively little programming experience and no algorithmic knowledge. Do as many as you want, then move on! You do not have to do all of them.

View file

@ -5,8 +5,6 @@ author: Darren Yao
description: Demonstrates how to read input and print output for USACO contests.
---
## Brief Descriptions
<resources>
<resource source="IUSACO" title="2.1 - Input and Output">module is based off this</resource>
<resource source="CPH" title="1.2 - Input and Output" starred>cin, getline, files</resource>
@ -17,7 +15,9 @@ description: Demonstrates how to read input and print output for USACO contests.
In most websites (such as CodeForces and CSES), input and output are **standard**.
### C++
<LanguageSection>
<CPPSection>
The [<iostream\>](http://www.cplusplus.com/reference/iostream/) library suffices.
@ -57,14 +57,14 @@ int main() {
}
```
### Java
</CPPSection>
<JavaSection>
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.
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.
<spoiler title="Standard I/O">
```java
import java.io.*;
import java.util.*;
@ -115,8 +115,6 @@ 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`.
@ -130,13 +128,17 @@ Here's a brief description of the methods in our `InputReader` class, with an in
| `pw.print()` | Prints the argument to designated output stream |
</JavaSection>
</LanguageSection>
## 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 USACO, input is read from a file called `problemname.in`. After the program is run, output must be printed to a file called `problemname.out`. Note that you'll have to rename the `.in` and `.out` files depending on 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`.
<LanguageSection>
## C++
<CPPSection>
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.
@ -168,12 +170,12 @@ int main() {
}
```
## Java
</CPPSection>
<JavaSection>
We can modify the template above to support file I/O.
<spoiler title="File I/O">
```java
import java.util.*;
import java.io.*;
@ -224,7 +226,6 @@ public class template {
}
```
</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.
@ -241,3 +242,9 @@ public static void main(String[] args) {
pw.close();
}
```
</JavaSection>
</LanguageSection>

View file

@ -11,9 +11,13 @@ Generally, input and output speed isn't an issue. However, some platinum tasks h
The largest USACO input file I know of is test case 11 of [USACO Platinum - Robotic Cow Herd](http://www.usaco.org/index.php?page=viewproblem2&cpid=674), which is 10.3 megabytes! The answer to this test case is $10^{18}$ (with $N=K=10^5$ and all microcontrollers costing $10^8$).
### C++
<LanguageSection>
`freopen` with `cin` was the slowest method.
<CPPSection>
### Method 1: `freopen` with `cin`
The slowest method.
<spoiler title="973ms">
@ -38,7 +42,9 @@ int main() {
</spoiler>
`freopen` with `scanf` is significantly faster.
### Method 2: `freopen` with `scanf`
Significantly faster.
<spoiler title="281ms">
@ -61,7 +67,9 @@ int main() {
```
</spoiler>
Using `ifstream` and `ofstream` is also about as fast.
### Method 3: `ifstream` and `ofstream`
About as fast.
<spoiler title="258ms">
@ -86,7 +94,9 @@ int main() { // 258 ms
</spoiler>
Of course, there are faster methods. For example, if we use FastIO from [here](https://github.com/bqi343/USACO/blob/master/Implementations/content/various/FastIO.h) then the runtime is further reduced.
### A Faster Method
I we use FastIO from [here](https://github.com/bqi343/USACO/blob/master/Implementations/content/various/FastIO.h) then the runtime is further reduced.
<spoiler title="91ms">
@ -110,7 +120,7 @@ int main() {
</spoiler>
#### Significance of `ios_base::sync_with_stdio(0); cin.tie(0);`
### Significance of `ios_base::sync_with_stdio(0); cin.tie(0);`
Adding this line as the first line of `main()` reduced the runtime of the first method to 254 ms.
@ -119,9 +129,14 @@ Adding this line as the first line of `main()` reduced the runtime of the first
<resource source="SO" url="https://stackoverflow.com/questions/31162367/significance-of-ios-basesync-with-stdiofalse-cin-tienull" title="Significance of [above]"> </resource>
</resources>
<br />
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.
### Java
</CPPSection>
<JavaSection>
Keep in mind that a Java program that only reads in $N$ and outputs a number takes 138ms on USACO.
@ -381,7 +396,9 @@ public class roboherd_is {
The most realistic input method to implement in contest is `BufferedReader`, as it is relatively fast for the amount of code necessary.
### Python
</JavaSection>
<PySection>
Faster than the first C++ method! Significantly less if $P$ does not need to be stored.
@ -402,15 +419,26 @@ else:
</spoiler>
</PySection>
</LanguageSection>
## Fast Output
In general, it may be faster to store the answer all in a single `string` (C++) or `StringBuffer` (Java) and outputting it with a single function call. This method avoids the overhead of calling an output method many times, especially if the output is generated in many parts.
### C++
<LanguageSection>
<CPPSection>
The CF blog mentioned above notes that when printing many lines in C++, it may be faster to use the newline character `\n` in place of `endl`. Output streams in C++ (such as `cout` and `ofstream`) are buffered, meaning that they don't immediately print their output, but store some of it. At some point, the buffer's contents are written (i.e. "flushed") to the output device, whether it be the standard output stream or a file. Buffering the output helps with efficiency if accessing the output device (like a file) is slow. Because `endl` flushes the output, it may be faster to use `\n` instead and avoid unnecessary flushes.
### Java
</CPPSection>
When printing to the standard output stream in Java, it is faster to use `new PrintWriter(System.out)` than `System.out`. The syntax for `PrintWriter` is equivalent to that of `System.out` so it should be easy to use. The only difference is that one has to call `.flush()` or `.close()` on the `PrintWriter` at the very end of the program in order to write the output.
<JavaSection>
When printing to the standard output stream in Java, it is faster to use `new PrintWriter(System.out)` than `System.out`. The syntax for `PrintWriter` is equivalent to that of `System.out` so it should be easy to use. The only difference is that one has to call `.flush()` or `.close()` on the `PrintWriter` at the very end of the program in order to write the output.
</JavaSection>
</LanguageSection>

View file

@ -81,7 +81,6 @@ One limitation of the ordered set is that we can't efficiently access the $k^{th
</LanguageSection>
## Ordered Maps
<LanguageSection>