From 72a3f2347a5d965888849b9e7e089104da10f818 Mon Sep 17 00:00:00 2001 From: nchn27 <46332369+nchn27@users.noreply.github.com> Date: Sat, 4 Jul 2020 00:51:54 -0400 Subject: [PATCH] Update IO_Speed.mdx --- content/2_General/IO_Speed.mdx | 68 +++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/content/2_General/IO_Speed.mdx b/content/2_General/IO_Speed.mdx index 8713468..06f96a7 100644 --- a/content/2_General/IO_Speed.mdx +++ b/content/2_General/IO_Speed.mdx @@ -118,12 +118,14 @@ int main() { The Java `Scanner` is probably the easiest way to read input in Java, though it is also extremely slow. +*Reading input with `Scanner`* ```java import java.util.*; import java.io.*; +//3188ms public class roboherd_scanner { static int P[][] = new int[100000][]; @@ -148,7 +150,71 @@ public class roboherd_scanner { -A combination of +A common alternative to reading input in Java for programming contests is `BufferedReader`, which reads faster from a file than `Scanner`. `BufferedReader` is used to read input line by line using the `.readLine()` method, which returns a string. Methods like `Integer.parseInt()` are used to convert strings into primitives usable in calculations, and can directly convert a line into a number, like in `Integer.parseInt(br.readLine())`. + +Reading input is more complicated when multiple, space-separated values are placed in a single line. In order to individually read the values in each line, the programmer usually uses the `.split()` method in `String` or the `.nextToken()` in `StringTokenizer`. Notice that using `StringTokenizer` to split strings is slightly faster than `.split()`. + +*Reading input with `BufferedReader` and `.split()`:* + +import java.util.*; +import java.io.*; +//1209ms + +public class roboherd_buffered_reader_string_split { + + static int P[][] = new int[100000][]; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new FileReader("roboherd.in")); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("roboherd.out"))); + + String[] tokens = br.readLine().split(" "); + int N = Integer.parseInt(tokens[0]); + int K = Integer.parseInt(tokens[1]); + for(int i = 0; i < N; ++i) { + tokens = br.readLine().split(" "); + int M = Integer.parseInt(tokens[0]); P[i] = new int[M]; + for(int j = 0; j < M; ++j) P[i][j] = Integer.parseInt(tokens[j+1]); + } + + if(N == 3) pw.println(61); + else pw.println(1000000000000000000L); + pw.close(); + } +} + + +*Reading input with `BufferedReader` and `StringTokenizer`* + +import java.util.*; +import java.io.*; +import java.awt.Point; +import java.math.BigInteger; +//986ms + +public class roboherd_buffered_reader_string_tokenizer { + + static int P[][] = new int[100000][]; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new FileReader("roboherd.in")); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("roboherd.out"))); + + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + for(int i = 0; i < N; ++i) { + st = new StringTokenizer(br.readLine()); + int M = Integer.parseInt(st.nextToken()); P[i] = new int[M]; + for(int j = 0; j < M; ++j) P[i][j] = Integer.parseInt(st.nextToken()); + } + + if(N == 3) pw.println(61); + else pw.println(1000000000000000000L); + pw.close(); + } +} + (custom I/O?)