-
-
Save jhyoty/bf5985cf3ec5646b606f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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