Блог пользователя Ahmad

Автор Ahmad, история, 7 лет назад, По-английски

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?

  • Проголосовать: нравится
  • -5
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

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.

»
5 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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