So I've written this up in Java and am getting mixed results. I don't quite know how to explain it. I have submitted it many times on INTEST, and about 20% of the time is gets ACC in ~1.85 seconds, and the rest of the time it gets WA. I'm at a loss. Anybody see anything wrong with this? I'm by no means an expert on efficient input in Java, so there may be something very stupid I'm doing here...
[code]
final int BUFSIZE = 1 << 16;
BufferedInputStream sin = null;
byte[] buffer = new byte[BUFSIZE];
int bufptr = 0;
void open() throws Exception
{
sin = new BufferedInputStream(System.in, BUFSIZE);
init();
}
void init() throws Exception
{
bufptr = 0;
sin.read(buffer, 0, BUFSIZE);
}
int nextInt() throws Exception
{
int ret = 0;
byte c = read();
boolean neg = false;
if (c == '-')
{
neg = true;
c = read();
}
if (c == '+')
c = read();
while (c <= ' ')
c = read();
ret = c - '0';
c = read();
while (c >= '0' && c <= '9')
{
ret *= 10;
ret += c - '0';
c = read();
}
if (neg)
return -ret;
return ret;
}
byte read() throws Exception
{
if (bufptr == BUFSIZE)
init();
byte c = buffer[bufptr++];
return c;
}[/code]