Skip to content

Instantly share code, notes, and snippets.

@jhyoty jhyoty/OutrunSolver.java Secret
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.