This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/1_Intro/Ex_Prob.mdx

203 lines
5.7 KiB
Text
Raw Normal View History

2020-06-03 22:48:48 +00:00
---
2020-06-16 01:06:49 +00:00
id: ex-prob
title: An Example Problem
2020-06-15 19:15:27 +00:00
author: Nathan Wang, Benjamin Qi, Darren Yao
2020-06-16 01:44:03 +00:00
prerequisites:
2020-06-22 20:51:12 +00:00
- Intro - Data Types
- Intro - Input & Output
2020-06-22 03:32:32 +00:00
description: Solutions for "USACO Bronze - Fence Painting" in multiple languages.
2020-06-03 22:48:48 +00:00
---
2020-06-24 21:28:57 +00:00
import { Problem } from "../models";
export const metadata = {
problems: {
2020-06-29 01:31:37 +00:00
fence: [
new Problem("Bronze", "Fence Painting", "567", "Intro"),
],
2020-06-24 21:28:57 +00:00
general: [
2020-06-24 22:31:52 +00:00
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"),
2020-06-24 21:28:57 +00:00
]
}
};
2020-06-29 20:20:17 +00:00
<resources>
<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>
2020-06-03 16:39:48 +00:00
2020-06-29 01:31:37 +00:00
## Example: Fence Painting
2020-06-03 16:39:48 +00:00
2020-06-29 01:31:37 +00:00
<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. **Make sure not to output trailing spaces!**
2020-06-08 21:11:15 +00:00
2020-06-29 20:51:53 +00:00
![bad](./Error.png)
2020-06-29 20:20:17 +00:00
2020-06-08 21:11:15 +00:00
### C++
2020-06-29 20:20:17 +00:00
[Here](https://www.geeksforgeeks.org/bitsstdc-h-c/) is some info about `<bits/stdc++.h>` if you are not familiar with it.
2020-06-09 00:47:37 +00:00
2020-06-08 21:11:15 +00:00
#### 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.
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("paint.in","r",stdin); // reuse standard in to read from "paint.in"
freopen("paint.out","w",stdout); // reuse standard out to write to "paint.out"
vector<bool> cover(100);
int a, b, c, d; cin >> a >> b >> c >> d;
for (int i = a; i < b; ++i) cover[i] = 1;
for (int i = c; i < d; ++i) cover[i] = 1;
int ans = 0;
for (int i = 0; i < 100; ++i) ans += cover[i];
2020-06-19 01:38:30 +00:00
cout << ans;
// cout << ans << endl; is OK
2020-06-09 00:47:37 +00:00
// cout << ans << "\n"; is OK
2020-06-19 01:38:30 +00:00
// cout << ans << " "; is NOT OK
// cout << ans << "\n\n"; is NOT OK
2020-06-08 21:11:15 +00:00
}
```
#### Method 2
Use [ifstream & ofstream](http://www.cplusplus.com/doc/tutorial/files/).
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream fin("paint.in");
ofstream fout("paint.out");
vector<bool> cover(100);
int a, b, c, d; fin >> a >> b >> c >> d;
for (int i = a; i < b; ++i) cover[i] = 1;
for (int i = c; i < d; ++i) cover[i] = 1;
int ans = 0;
for (int i = 0; i < 100; ++i) ans += cover[i];
2020-06-19 01:38:30 +00:00
fout << ans;
2020-06-08 21:11:15 +00:00
}
```
### Java
```java
import java.io.*;
import java.util.*;
2020-06-09 00:47:37 +00:00
public class paintSol { // must be declared in paintSol.java
2020-06-08 21:11:15 +00:00
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("paint.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("paint.out")));
int[] cover = new int[100];
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int c = Integer.parseInt(st.nextToken()), d = Integer.parseInt(st.nextToken());
2020-06-15 19:15:27 +00:00
for (int i = a; i < b; i++) cover[i] = 1;
for (int i = c; i < d; i++) cover[i] = 1;
2020-06-08 21:11:15 +00:00
int ans = 0;
2020-06-15 19:15:27 +00:00
for (int i = 0; i < 100; i++) ans += cover[i];
2020-06-08 21:11:15 +00:00
pw.println(ans);
2020-06-15 19:15:27 +00:00
pw.close(); // make sure to include this line -- flushes the output.
2020-06-08 21:11:15 +00:00
}
}
```
2020-06-19 01:15:13 +00:00
Alternatively, we can use the InputReader given in the previous module.
2020-06-29 20:20:17 +00:00
<spoiler title="Solution with InputReader">
2020-06-16 01:44:03 +00:00
2020-06-15 19:34:27 +00:00
```java
2020-06-15 19:15:27 +00:00
import java.util.*;
import java.io.*;
public class template {
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader() throws FileNotFoundException {
2020-06-19 01:15:13 +00:00
reader = new BufferedReader(new FileReader("paint.in"));
2020-06-15 19:15:27 +00:00
tokenizer = null;
}
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
InputReader r = new InputReader();
2020-06-19 01:15:13 +00:00
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("paint.out")));
2020-06-15 19:15:27 +00:00
int[] cover = new int[100];
for (int i = a; i < b; i++) cover[i] = 1;
for (int i = c; i < d; i++) cover[i] = 1;
int ans = 0;
for (int i = 0; i < 100; i++) ans += cover[i];
pw.println(ans);
2020-06-15 19:16:47 +00:00
pw.close(); // flush output
2020-06-15 19:15:27 +00:00
}
}
```
2020-06-19 01:15:13 +00:00
</spoiler>
2020-06-08 21:11:15 +00:00
### Python 3
2020-06-19 01:15:13 +00:00
See [here](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) for documentation about file I/O.
2020-06-08 21:11:15 +00:00
```py
fin = open("paint.in","r")
fout = open("paint.out","w")
cover = [0 for i in range(100)]
a,b = map(int,fin.readline().split())
c,d = map(int,fin.readline().split())
for i in range(a,b):
cover[i] = 1
for i in range(c,d):
cover[i] = 1
ans = 0
for i in range(100):
ans += cover[i]
fout.write(str(ans))
```
2020-06-16 01:06:49 +00:00
## Introductory Problems
2020-06-03 16:39:48 +00:00
2020-06-16 01:06:49 +00:00
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.
2020-06-08 21:11:15 +00:00
2020-06-24 21:28:57 +00:00
<problems-list problems={metadata.problems.general} />
2020-06-24 20:50:30 +00:00
Also check the [CSES Introductory Problems](https://cses.fi/problemset/list/) up to and including "Palindrome Reorder."