Uploaded by br o

study guide usaco

advertisement
Study Guide
● Most important link https://officialguide/contests?fake=true
● Don’t use USACO training pages
● Know the standard library - java: set, map, queue/pq/stack, binary search,
arrayList/collections
● https://usaco-guide.vercel.ap also include codeforces problems
● https://cses.fi/book/book.pdf but its for c++
● https://link.springer.com/book/10.1007/978-3-319-72547-5 just a list of basics
● https://www.csc.kth.se/~jsannemo/slask/main.pdf silver and a little bit of gold
● https://darrenyao.com/usacobook/java.pdf covers everything in silver, even the ad hoc
problems like the two pointers and that one line sweep problem - Cow Steeplechase II
● For algorithms
https://www.geeksforgeeks.org/top-algorithms-and-data-structures-for-competitive-progr
amming/ and https://cp-algorithms.com/ is a comprehensive list of algorithms
● Problemsets - https://cses.fi/problemset/,
https://www.hackerrank.com/domains/algorithms, codeforces, and USACO ofc
● Read the editorials after 30 minutes from reading if you don’t know
Other info
● USACO only takes the last submission so back up your code
● Test cases are always inadequate so create larger cases that might give more insightful
information
● Graph the test cases out; use the links below
Lessons
● Stop adding in loops to get a value, just multiply or divide and get it done in O(1)
● Look for problems that just want the end result (“yes”/”no”) and don’t care about the
process (especially for problems that seem to require a simulation, but in reality, you can
just skip the simulation and get the answer by using another method)
● For problems with swaps, find the loops or just find the locations with 0 on the first pass check out Hoofball (bronze) and Convention II (silver)
●
Notes:
● DSU is much faster than floodfill after optimizations
● RMQ and prefix sums - look for these when you are told to find a distance or amount
between two points
● Graph and find relationships between variables (increasing/decreasing for binary search)
● Coordinate compression with endpoints list(1D/2D)
● Prefix sums
● Linear sweep
● DFS/BFS
● Check if graphs are bipartite, two-coloring - graphs are non-bipartite if there are no odd
cycles
● Identify whether problem is tree or graph
● Invariants in transformations
●
●
●
●
●
Loops in swapping elements
LCA - rarely need it
Identify connected components/SCC
1+2...N = N^2/2; 1 + 2 … 2N^0.5 = N
For problems where you distribute like food or something between people, you can try
finding the minimum amounts to distribute, then increase it instead of trying to find the
highest to give to one group and shaving off some of that if it is too large
Ad Hoc (no specific algorithm just mind games)
● Silver (this is from a list somebody made)
-Circular Barn(2016 February)
-Milk Pails(2016 February)
○ Note: This problem can be also solved using dfs over all possible states.
-Field Reduction(2016 US Open)
-Secret Cow Code(2017 January)
-Grass Planting(2019 January)
-Berry Picking (2020 January)
○ Note: Emphasis on some math
-Sleepy Cow Herding(2019 February)
-Meetings (2019 December)
○ Note: Particularly difficult
-Left out(2019 US Open)
-Cereal(2020 Open)
Coding Tips (if you are not in a codeforces contest where you need speed)
● Write as many functions - makes it easy to debug if you can guarantee that some are
100% correct
● Put comments for nested if statements
● Use if statements and array buffers if you need to for the cases where like i = 0 or i N - 1,
but be careful with array buffers because changing the size of your array can mess up
some other part of your program
● Print to console all the variables you can or constructed arrays
Sites to use in contest
● https://www.desmos.com/calculator for functions and points
● https://csacademy.com/app/graph_editor/ to generate graphs
● https://oeis.org/ find the integer sequence
Code snippets to save time while coding or so you don’t have to google every time (you need to
edit the capitalized parts)
● Input Template (from the Darren Yao’s book) - note that you don’t have to wrap the
FileWriter in BufferedWriter because PrintWriter already uses buffered output
public class template {
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader() throws FileNotFoundException {
reader = new BufferedReader(new FileReader("INPUT_FILE.in"));
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()); }
}
}
}
Static InputReader ir = new InputReader();
Static PrintWriter pw = new PrintWriter(new FileWriter("OUTPUT_FILE.out"));
● Sort and Array Operationsa. Comparator without class
Comparator<CLASS> comparator = new Comparator<CLASS>() {
public int compare(final CLASS a, final CLASS b)
{
if(a > b)
return 1;
else if(a < b)
return -1;
else
return 0;
}
};
Arrays.sort(ARR, comparator);
b. Sort lexicographically
Arrays.sort(ARR, Arrays::compare);
c.
Reverse Array
● Hashmap
a. Increment value
public static<K> void incrementValue(Map<K,Integer> map, K key)
{
AtomicInteger atomic = new AtomicInteger(map.containsKey(key) ? map.get(key) : 0);
map.put(key, atomic.incrementAndGet());
}
Download