Ahmad's blog

By Ahmad, history, 8 years ago, In English

I recently solved 219C - Color Stripe (Submission) and I can't seem to get it to run in under a second in Java.

I've looked through faster Java submissions and still have no idea why mine takes a second to a second and a half longer than theirs. Is there something obvious that I'm missing?

  • Vote: I like it
  • -5
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it +9 Vote: I do not like it

I bet most of your program's time is spent in IO operations. PrintWriter seems to flush whenever it decides it is necessary. To be more precise, it flushes its buffer after each call to out.printf("%c", a[i]);. As a workaround, you may try creating a new String from the char array and printing it. As a preferred solution, get rid of PrintWriter and use BufferedReader.

P.S. you may want to use Java profiler in an IDE to find bottlenecks if nothing else helps.

»
6 years ago, # |
  Vote: I like it +1 Vote: I do not like it

This is 2 years late but, best is to do out.println(new String(char[] c));