_Comfortably_Numb__'s blog

By _Comfortably_Numb__, history, 7 years ago, In English

In problem 220B - Little Elephant and Array 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.

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

  • »
    »
    7 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. :(

    • »
      »
      »
      7 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.

      • »
        »
        »
        »
        7 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.

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