When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

_Comfortably_Numb__'s blog

By _Comfortably_Numb__, history, 6 years ago, In English

In problem 220B - Маленький Слоник и массив my submission 36377029 got WA. Then I comment ios_base::sync_with_stdio(0) and submit the same code 36377330 again. It got accepted. Now I wonder why my first code got WA and when we should use ios_base::sync_with_stdio(0) and when we shouldn't.

»
6 years ago, # |
  Vote: I like it +1 Vote: I do not like it

I think you only use it if you are using cin and cout. Don't mix cin/scanf/cout/printf or weird things can happen.

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

    I submitted this 36376045 with ios_base::sync_with_stdio(0) , cin and cout. No scanf, printf. But it got TLE. And in this 36378988 submission I add cout.tie(0) with them. But
    it got WA. :(

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

      On your second sumbission you forget to delete scanf and printf, that's why you got WA.

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

        Thanks. cin.tie(0) works 36386259.

        But still wonder why ios_base::sync_with_stdio(0) without cin.tie(0) gives WA in this problem.

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

I would also add that this optimisation only makes sense if your input or output have more than a constant amount of characters (e.g. if there is a valid test in which any of these reach at least a thousand), because otherwise the time you spend on executing this function overweights the time you save from input/output.

You can also use cin.tie(0) and cout.tie(0) in addition to this, they as well speed things up a bit.