checkMate09's blog

By checkMate09, history, 9 years ago, In English

Hey codeforces , I was solving C. Replacement. my first submission got Accepted with awful time 1996 ms -> My first Submission
so i was looking to reduce it once using cin.tie and it doesn't work cin.tie(0) submission another way that i tried is to use scanf instead of cin and the code didn't output even sample answers correctly. and here is the strangest thing that i re-send the accepted code it gives TLE now!! so, any coding technique to reduce the time? why is my cin.tie submission Run time error? why my identical accepted submission got TLE ? Thanks.

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

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

Never use scanf with cin.tie(0). Source

AC with cin.tie(0)

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

    thank you for your reply instead of throwing negative votes. and for your help accepted with reduced time 1652 ms still awful :D (y)

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

      Welcome. :)

      Same code with scanf and printf(171ms).

      Code

      PS — I am too surprised of this huge difference!

      PPS — Nope, now I am not. :-P You used endl which is slow. Code (436ms).

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

The problem is not the use of cin/cout, but rather the use of endl. It is not just a simple newline character. It also flushesh the whole stream which causes bad performance. Try using '\n' instead of endl in such problems where there are many lines of output