osalbahr's blog

By osalbahr, history, 2 years ago, In English

Is there a reason why I needed to fflush( stdout ) in the sample interactive problem even though I printed a newline in the printf? When I run the code locally, it does flush automatically, as expected.

Without fflush: Idleness limit exceeded on test 1

https://codeforces.me/gym/101021/submission/159265004

With fflush: Accepted

https://codeforces.me/gym/101021/submission/159265085

Shouldn't they be equal? I am not sure if this is a misunderstanding on my behalf, or an issue in the interactor (at least for GNU C11).

(Comment in context https://codeforces.me/blog/entry/45307#comment-918362)

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It's not strange, here is the explanation:

Most languages keep collecting the output, printing all the collected output in one go at the end of the program to improve efficiency. But this may cause us trouble with Interactive problems as we want our output to be sent to output console before terminating the program.

Fortunately, we have tools for this, called manually flushing output. Flushing output means, that you ask your program to print all buffered output and clear the buffer. This way, the output is sent to console before termination of the program, and only then we can receive input from the grader program since it returns the next input only after receiving previous output.

Read more here.