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

Автор NPA, 11 лет назад, По-английски

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.me/contest/546/submission/11225195

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

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

Теги c++, io
  • Проголосовать: нравится
  • -15
  • Проголосовать: не нравится

»
11 лет назад, скрыть # |
 
Проголосовать: нравится -55 Проголосовать: не нравится

Iostream=evil. You never know when will bring.

»
11 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +32 Проголосовать: не нравится

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

»
11 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +5 Проголосовать: не нравится

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).

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
11 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится -13 Проголосовать: не нравится

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.