Milil's blog

By Milil, history, 6 months ago, In English

Hey, dudes and chicks.

A quick notion: the issue I describe here concerns C++ code, but if the same issue is observed in other languages, be welcome to comment on it.

Recently I participated in a local bootcamp organized by my fellows from the university — it included a contest with the problems designed by the organizers meaning that the statements and the tests were written solely by them and they are only accessible via a private group contest so I can't share a link to them here (overall difficulty was somewhere between Div. 3 and Div. 2 if you wonder). One of the problems turned out to be tricky in an unpleasant way — after squeezing out all the possible time-complexity-related optimizations and still getting a TLE, we got a message from the jury (who must have seen the submission and figured that our approach was correct) that the fix we needed was to add the lines std::cin.tie(0); std::ios_base::sync_with_stdio(0); at the beginning of our code. After we did this, we got our long awaited Accepted verdict.

Before that, I thought that these I/O-related TLEs were a thing of the past — I still remember some contests for schoolkids where I/O was a TL bottleneck, but it was way back in 2016 or so — sometimes you needed to use stdio instead of iostream (printf instead of cout) and if you did not, you risked getting a TL with an otherwise correct solution.

So, I wonder a few things after that. First, is it still preferred to this day to add those I/O speedups? If so, what is the single best way to do this — can you rely on std::cin.tie(0); std::ios_base::sync_with_stdio(0); or is it better to use printf and scanf instead of generally more convenient iostream tools? Secondly, this issue does not seem to depend on the architecture of testing servers as this particular issue happened like 2 months ago, so does it depend on the specific set of tests for the problem and how then to figure out from the problem constraints that the tests may include those with I/O bottlenecks? Finally, does the same issue appear in other languages besides C++ and if it does, what are the general guidelines to avoid these traps?

Appreciate all valuable input, thank you!

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
6 months ago, # |
  Vote: I like it +16 Vote: I do not like it

I believe adding cin.tie(0)->sync_with_stdio(0);(my preference compared to std::cin.tie(0); std::ios_base::sync_with_stdio(0); even though there's no difference) makes cin and cout just as fast as scanf and printf.

  • »
    »
    6 months ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    This makes cin faster than scanf if you are submitting C++20(64-bits) on codeforces.