Written by Johannes Kapfhammer.
Contents
General Remarks
Java is supported for the finals and team selection of SOI, even though it is not anymore available at IOI. We guarantee that all tasks from the finals can be solved for 100 points in Java.
To run Java on the SOI grader, there’s a few points to consider:
- We run Java 11 on OpenJDK (Java 11.0.12, OpenJDK Runtime Environment, OpenJDK 64-Bit Server VM).
- You need to submit a single file to be compiled as “submission.java”. Therefore the outermost class has to be called “submission” (note the lowercase letters).
- Since Java can be a bit slower and more memory-hungry than C++, you will have twice the time and memory available. If, for example, it says on some task “Time limit: 1000ms, memory limit: 256MiB”, then Java will have a time limit of 2000ms and memory limit of 512MiB.
- Input with the Scanner class is quite slow, even with the increased time limit an otherwise good solution might not pass. So we recommend using BufferedReader and StringTokenizer instead (see template below).
- If possible, use built-in arrays. They are by far the fastest way to store lists. For dynamic arrays (e.g. for adjacency lists in graphs), you can use ArrayList<> (stay away from LinkedList<>, ArrayList<> is better in pretty much all situations).
SOI Java Template
Although you are free to code however you like, we recommend using the template above when solving tasks in Java.
For faster I/O, this uses BufferedReader and StringTokenizer and then manually converts strings to ints. For easier usage, it wraps this in a class InputReader that behaves like the Scanner class and contains the two methods next (for reading a string), nextInt (for reading an int), nextLong (for reading a long).
When using this template, all you need to implement is the method solve in the class Task. Note that all classes except submission are package-private.
Feel free to make use of member variables and methods inside the class Task. The following is a perfectly valid solution for the task addition:
class Task { long a; long b; public void solve(InputReader in, PrintWriter out) { a = in.nextLong(); b = in.nextLong(); out.println(add()); } long add() { return a + b; } }