Skip to content

Instantly share code, notes, and snippets.

@jhyoty
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhyoty/bf5985cf3ec5646b606f to your computer and use it in GitHub Desktop.
Save jhyoty/bf5985cf3ec5646b606f to your computer and use it in GitHub Desktop.
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
public class OutrunSolver {
public static void solve(Path input) throws FileNotFoundException, IOException {
final long size = Files.size(input);
final MappedByteBuffer bytes;
try (FileChannel fc = FileChannel.open(input, StandardOpenOption.READ)) {
bytes = fc.map(FileChannel.MapMode.READ_ONLY, 0, size);
}
while (bytes.get() != '\n');
boolean number = false;
int a = 0;
int[] buf = new int[1024];
int left = 0;
int pos = 0;
int line = 0;
int max = 0;
while (bytes.hasRemaining()) {
final int b = bytes.get();
if (b == ' ') {
if (number) {
final int right = buf[pos];
final int n = a + (left > right ? left : right);
left = right;
buf[pos] = n;
a = 0;
number = false;
pos++;
}
continue;
}
if (b == '\n') {
if (number) {
final int right = buf[pos];
final int n = a + (left > right ? left : right);
buf[pos] = n;
a = 0;
number = false;
}
pos = left = 0;
line++;
if (buf.length <= line) {
buf = Arrays.copyOf(buf, buf.length * 2);
}
continue;
}
number = true;
a = a * 10 + b - '0';
}
for (int i = 0; i < line; i++) {
if (buf[i] > max) {
max = buf[i];
}
}
System.out.println(max);
}
public static void main(String[] args) throws IOException {
OutrunSolver.solve(Paths.get(args[0]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment