NPA's blog

By NPA, 9 years ago, In English

Last round I submitted problem D using cin/cout + ios::sync_with_stdio(false) but it turned out to be too slow (TLE on test 3). Then I try to put cin.tie(0) and got Accepted in 2 seconds. Anyone have any idea why cin.tie(0) is necessary in this case ?

My submissions:

http://codeforces.com/contest/546/submission/11225195

http://codeforces.com/contest/546/submission/11224645

UPD: Explained by rlac and alternative fix by vadimmm in the comment below.

Tags c++, io
  • Vote: I like it
  • -15
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it -55 Vote: I do not like it

Iostream=evil. You never know when will bring.

»
9 years ago, # |
Rev. 2   Vote: I like it +32 Vote: I do not like it

By default, before you call the function cin, the call cout << flush is done. So, in your first submission there isn't any difference between cout << endl and cout << "\n".

You can override this behavior by calling the function cin.tie(0).

I hope this can help you

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    what is cout<<flush

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

      It forces the output buffer to be flushed. When you say cout << X, X isn't automatically printed automatically to the console, it is stored in a buffer and then, when the buffer is flushed is that actually X is printed in the console (this is what actually makes output operations to be slow).

      • »
        »
        »
        »
        9 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        Hello rlac and NPA

        cin.tie did not help me.

        See this submission without cin.tie: TLE

        11228080

        This one is with cin.tie: TLE

        11228099

        And this one is with scanf/printf: Accepted

        11228062

        • »
          »
          »
          »
          »
          9 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I think your sieve is too slow (see the runtime of test 1 and test 2).

        • »
          »
          »
          »
          »
          9 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Try adding ios::sync_with_stdio(false) as well (in the same place as cin.tie(0)).

  • »
    »
    9 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Nice explanation !

    Thanks, now it seems that cin.tie(0) should always be included in the code.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Omg, I've always been using iostream without knowing this :o. One can learn something even in Div2 contests, I see :P.

»
9 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

If you write some_stream.tie(ostream_ptr), the tied output stream (references by ostream_ptr) is flushed before each i/o operation in some_stream.
By default std::cin and std::cerr are tied to std::cout.
So avoid alternation of input and output (11226406), or use std::cin.tie(nullptr).

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

    Thank you for your help, I learned a lot from your comment.

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

Also scanf and printf are faster compared to iostream. It depends on the compiler but that's the case in codeforces.

»
9 years ago, # |
Rev. 2   Vote: I like it -13 Vote: I do not like it

A lot of people in round's blog's comment section also talks about it. And it really annoys me. Please stop.

Your solution is not as fast as the intended one, don't blame cin/cout or authors.

It's just silly.

UPD: I'm sorry. I was wrong. It turns out that even intended solution gets TLE because of it.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I just wanna know why my code failed :|

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it -8 Vote: I do not like it

      I know. It wasn't something personal. It was general and I thought this blog would be better for everyone to see it. I was wrong anyway.