kartikea_gupta's blog

By kartikea_gupta, history, 6 weeks ago, In English

I submitted this solution to Problem A of Round #663. I know the problem was really easy and even my logic was correct. What actually caused problems was including the following two line in my void solve() function.

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

I added the two lines for fast input/output through cin/cout but mistakenly added them to void solve(). However, after removing these two lines the code worked fine and got accepted.
Link to first solution.
Link to second solution.

Can someone please explain what exactly went wrong and why?
Thanks in advance!!
UPD: Not here for contributions. Plz just answer it if you know the answer.

 
 
 
 
  • Vote: I like it
  • -15
  • Vote: I do not like it

»
6 weeks ago, # |
Rev. 4   Vote: I like it +1 Vote: I do not like it

I moved the 2 lines in the beginning of main(), and it got accepted. Putting those 2 lines inside main() like this guarantees that they run only once. You put those inside your solve() function, which will be run multiple times.

IIRC, it resets something but I can't remember (is it rdbuf?). Idk, I'm just gonna wait here until someone comes with a more detailed answer.

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

    Yeah, ik that the two lines are to be meant for int main(). My previous solutions might show that i keep them in main(). Something bizarre went when i kept them in solve() though.

»
6 weeks ago, # |
  Vote: I like it +28 Vote: I do not like it

Everything went wrong when you copied that template

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

    Lol, it ain't copied. I copied the two lines for fast input/output. Brave of you to assume that.

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

    Also, why don't you check out all my previous submissions if you think the template has been copied?

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

      and why she should do that?

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

        Okay, my bad to say so. I apologize for the same. Can we please just focus on the question?

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

        ????? because shes the one accusing him of something

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

Auto comment: topic has been updated by kartikea_gupta (previous revision, new revision, compare).

»
6 weeks ago, # |
Rev. 3   Vote: I like it +8 Vote: I do not like it

Your code calls ios_base::sync_with_stdio(false) after I/O (input/output) (e.g. cin/cout) has been used. This results in implementation-defined behavior, which in this case results in undefined behavior as I/O stops working.

If this function is called after I/O has occurred on the standard stream, the behavior is implementation-defined: implementations range from no effect to destroying the read buffer.

Your code should call ios_base::sync_with_stdio(false) before any I/O occurs. So it should be at the beginning of the main() function.